简体   繁体   English

无法调用引用实现特征的类型的函数

[英]Unable to call a function with a reference to a type implementing a trait

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 : 但是当我尝试使用get创建特征(此处为XT )时:

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 没有为&T类型实现特征XT

It works if I change f2(&x) to f2(x) . 它的工作原理,如果我改变f2(&x)f2(x) My expectation was that replacing the types by the traits, everything will work. 我的期望是,用特征替换类型,一切都会正常。

The problem is that you're trying to pass &&T to f2 . 问题是您正在尝试将&&T传递给f2 That means it expects &T to implement XT , and that's not what you said: you said that T implements XT . 这意味着它期望&T实现XT ,但这不是您所说的:您说T实现了XT

You can modify f1 to properly express this constraint by using a where T: XT, for<'a> &'a T: XT clause, but then you can't call f1 from main because &X doesn't implement XT . 您可以使用where T: XT, for<'a> &'a T: XT子句来修改f1以正确表达此约束,但是由于&X没有实现XT ,因此您不能从main调用f1 So you go and add an implementation for that as well , and then the code works... but honestly, it's easier to just remove that & and call f2(x) instead. 所以,你去添加一个实现该为好然后代码工作......但说实话,它更容易只是删除&和调用f2(x)来代替。

To put it another way: just because a type implements a trait does not mean pointers to that type also implement the trait. 换句话说,仅仅因为类型实现了特征并不意味着指向该类型的指针实现了特征。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为类型引用实现特征时返回对临时值的引用时出错 - Error returning reference to a temporary value when implementing a trait for a type reference 实现一个特征方法,返回一个拥有类型的有界生命周期引用 - Implementing a trait method returning a bounded lifetime reference for owned type 实现 trait 或取消引用 trait 的类型的通用类型 - Generic type for types implementing trait or dereferencing to trait 实施借阅 <Trait> 对于实现Trait的类型 - Implementing Borrow<Trait> for a type that implements Trait 如何指定一个带引用的闭包,并返回实现与引用具有相同生命周期的特征的任何类型? - How do I specify a closure that takes a reference and returns any type implementing a trait with the same lifetime as the reference? 为具有生命周期的类型实现 Borrow trait - Implementing Borrow trait for a type with a lifetime 为实现您的特征的任何类型实现外来特征 - Implementing a foreign trait for any type implementing your trait 在Rust特性中实现构造函数 - Implementing constructor function in rust trait 实现一个返回泛型的特征函数 - Implementing a trait function that returns generic 如果生命周期未被使用,为什么在引用类型上实现特征时需要生命周期,在Rust <1.31? - Why is a lifetime needed when implementing a trait on a reference type if the lifetime is otherwise unused, in Rust < 1.31?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM