I am having some trouble to understand how to work with Traits and ownerships. The following example works:
struct X([u8; 4]);
impl X {
pub fn get(&self, n: usize) -> u8 {
self.0[n]
}
}
fn f1(x: &X) {
println!("{}", x.get(1));
f2(&x);
}
fn f2(x: &X) {
println!("{}", x.get(2));
}
fn main() {
let z1 = X([1u8, 2u8, 3u8, 4u8]);
f1(&z1);
}
But when I try to create a trait (here XT
) with get
:
trait XT {
fn get(&self, n: usize) -> u8;
}
struct X([u8; 4]);
impl XT for X {
fn get(&self, n: usize) -> u8 {
self.0[n]
}
}
fn f1<T: XT>(x: &T) {
println!("{}", x.get(1));
f2(&x);
}
fn f2<T: XT>(x: &T) {
println!("{}", x.get(2));
}
fn main() {
let z1 = X([1u8, 2u8, 3u8, 4u8]);
f1(&z1);
}
Fails to compile with the following error message:
the trait
XT
is not implemented for the type&T
It works if I change f2(&x)
to f2(x)
. My expectation was that replacing the types by the traits, everything will work.