【发布时间】:2014-12-08 02:00:37
【问题描述】:
我不太明白下面的代码有什么问题。反正也不是很清楚。我曾经用一生来参数化 Toto,但我想我会试一试终身推断。问题似乎与对 self 的引用有关。我得到编译器错误:
embedded_lifetimes.rs:11:5: 11:10 error: cannot infer an appropriate lifetime for automatic coercion due to conflicting requirements
embedded_lifetimes.rs:11 slice
^~~~~
embedded_lifetimes.rs:10:3: 12:4 help: consider using an explicit lifetime parameter as shown: fn klax<'a>(&'a self, slice: &'a [String]) -> &[String]
embedded_lifetimes.rs:10 fn klax(&self, slice: &[String]) -> &[String] {
embedded_lifetimes.rs:11 slice
embedded_lifetimes.rs:12 }
对于以下代码:
#![feature(slicing_syntax)]
trait Toto {
fn klax(&self, &[String]) -> &[String];
}
struct Tata;
impl Toto for Tata {
fn klax(&self, slice: &[String]) -> &[String] {
slice
}
}
fn main() {
let t = Tata;
t.klax(&["myello".to_string()]);
}
【问题讨论】:
标签: rust