I am trying to avoid specifying multiple traits on each use of generic type T. Can I use the newtype idiom to specify bounds once? This works but I'll have the where ...
on each function.
fn prn<T>(arg: T)
where
T: std::fmt::Debug + Copy,
{
println!("{:?}", arg);
}
fn main() {
let x: i32 = 1;
prn(x);
}
Would it be possible to use a newtype like below to let me make fn prn
generic and not have to specify all the traits on each impl?
pub struct MyData<T>(T)
where
T: std::fmt::Debug + Copy + Clone + std::ops::Add<Output = T>;
My failed attempt below:
// WRONG - won't compile - is there syntax for newtype as type param?
fn prnt<MyData<T>>(arg: T) {
println!("{:?}", arg);
}
fn main() {
let x: i32 = 1;
prnt(x);
}