简体   繁体   English

Rust:“该特征没有为 Self 实现”,即使它完全是

[英]Rust: "the trait is not implemented for Self" even though it totally is

Look at this little snippet:看看这个小片段:

pub trait TestTrait {
    fn test_function(&self) {
        generic_function(self);
    }   
}

fn generic_function<T : TestTrait> (x : T) {
    //do something
}

This produces this error message:这会产生以下错误消息:

error[E0277]: the trait bound `&Self: TestTrait` is not satisfied
  --> src\main.rs:10:26
   |
10 |         generic_function(self);
   |         ---------------- ^^^^ the trait `TestTrait` is not implemented for `&Self`
   |         |
   |         required by a bound introduced by this call
   |
note: required by a bound in `generic_function`
  --> src\main.rs:14:25
   |
14 | fn generic_function<T : TestTrait> (x : T) {
   |                         ^^^^^^^^^ required by this bound in `generic_function`

But this doesn't make any sense.但这没有任何意义。 The test function is a method associated with the trait TestTrait . test function是与特征TestTrait关联的方法。 Of course, the self is going to implement this trait, that's the entire point!当然,self 会实现这个 trait,这就是重点!

What's going on?这是怎么回事? How to make this work?如何使这项工作?

generic_function takes an object by value. generic_function按值获取 object。 The function generic_function gets a reference to self and does not own self so is not able to give self by value to generic_function . function generic_function获得对self的引用并且不拥有self因此无法将self按值赋予generic_function

There are 2 simple fixes.有 2 个简单的修复。 Note that both require the constrain TestTrait where Self: Sized .请注意,两者都需要约束TestTrait where Self: Sized

1: generic_function takes the argument by reference 1: generic_function通过引用获取参数

pub trait TestTrait where Self: Sized {
    fn test_function(&self) {
        generic_function(self);
    }   
}

fn generic_function<T : TestTrait> (x : &T) {
    //do something
}
  1. test_function takes self by value test_function按值self
pub trait TestTrait where Self: Sized {
    fn test_function(self) {
        generic_function(self);
    }   
}

fn generic_function<T : TestTrait> (x : T) {
    //do something
}

暂无
暂无

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

相关问题 即使实现了Trait &lt;&str&gt;,为什么仍无法满足特质绑定的Trait &lt;&String&gt;? - Why is the trait bound Trait<&String> not satisfied even though I've implemented Trait<&str>? 为什么即使T实现了特征,也出现错误“未对&mut T实现特征Foo”? - Why do I get the error “the trait `Foo` is not implemented for `&mut T`” even though T implements the trait? Rust - 没有为 `OsString` 实现特征`StdError` - Rust - the trait `StdError` is not implemented for `OsString` 如何在Rust中实现特征对象? - How are trait objects implemented in Rust? 为什么即使它实现了 IntoIterator,对于泛型类型的引用,我也会收到错误“特征 `Iterator` 未实现”? - Why do I get the error “the trait `Iterator` is not implemented” for a reference to a generic type even though it implements IntoIterator? 绑定`&str的特征:从 <MyType> 即使我为MyType实现了From &lt;&str&gt;,也不满意 - The trait bound `&str: From<MyType>` is not satisfied even though I implemented `From<&str> for MyType` Rust actix-web:特征 `Handler&lt;_, _&gt;` 未实现 - Rust actix-web: the trait `Handler<_, _>` is not implemented Rust ndarray 类型错误:未实现特征绑定 - Rust ndarray type error: trait bound not implemented Rust“性状`借<char> ` 没有为 `&amp;str` 实现“错误</char> - Rust "the trait `Borrow<char>` is not implemented for `&str`" Error 在 Rust 中,如何使用 self 制作 trait 的 vec? - In Rust, how to make an vec of trait with self?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM