如何解决此错误?当我在impl.
struct LineHandlerInfo<'a> {
label: &'a str,
match_literal: &'a str,
f: fn(&str) -> Option<&str>,
}
struct Game<'a> {
handlers: Vec<LineHandlerInfo<'a>>,
}
impl Game<'_> {
fn match_str<'a>(
&'a mut self,
label: &'a str,
match_literal: &'a str,
mut f: fn(&str) -> Option<&str>,
) {
let mut lh = LineHandlerInfo {
label,
match_literal,
f,
};
self.handlers.push(lh);
}
}
fn main() {
let mut g = Game {
handlers: Vec::new(),
};
g.match_str("echo hello", "hello", |s| {
println!("{}", s);
None
});
}
Run Code Online (Sandbox Code Playgroud)
当我尝试编译时,出现以下错误:
struct LineHandlerInfo<'a> {
label: &'a str,
match_literal: &'a str,
f: fn(&str) -> Option<&str>,
}
struct Game<'a> {
handlers: Vec<LineHandlerInfo<'a>>,
}
impl Game<'_> {
fn match_str<'a>(
&'a mut self,
label: &'a str,
match_literal: &'a str,
mut f: fn(&str) -> Option<&str>,
) {
let mut lh = LineHandlerInfo {
label,
match_literal,
f,
};
self.handlers.push(lh);
}
}
fn main() {
let mut g = Game {
handlers: Vec::new(),
};
g.match_str("echo hello", "hello", |s| {
println!("{}", s);
None
});
}
Run Code Online (Sandbox Code Playgroud)
我如何解决这个错误,impl Game当我在结构上已经有一个生命周期时,当我指定一个生命周期时,我到底告诉编译器什么?
我如何解决这个错误,当我在结构上已经有一个生命周期时,当我在 impl Game 上指定一个生命周期时,我到底告诉编译器什么?
我怀疑您的困惑源于对 Rust 中声明和使用生命周期的方式的不完整理解。
为了在结构上使用生命周期,您可以在与您声明的<>结构名称相邻的位置声明生命周期,然后在结构定义中引用该生命周期。重要的是,请注意,在那里声明的生命周期仅限于结构体定义——它在外面没有任何意义。
例如(使用@Shepmaster 提供的 MRE):
struct Game<'a> {
handlers: Vec<&'a str>,
}
Run Code Online (Sandbox Code Playgroud)
该结构Game包含对字符串的引用向量,被引用的字符串必须至少与Game结构一样长。
在 impl 块上使用生命周期说明符时,您可以在与 impl 关键字相邻的内部声明生命周期<>,之后您可以在正在实现的结构中和在实现本身内部引用生命周期,如下所示:
impl<'b> Game<'b> {
fn match_str(&mut self, label: &'b str) {
self.handlers.push(label);
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,我在这里使用了一个完全不同的生命周期名称 ( 'b) 来说明结构上的生命周期声明独立于 impl 块上的生命周期声明。
打破这个:
impl<'b>
Run Code Online (Sandbox Code Playgroud)
这意味着我们正在为结构定义一个实现,并且在该定义中我们将使用生命周期 'b
Game<'b> {
Run Code Online (Sandbox Code Playgroud)
这意味着 impl 用于Game具有生命周期的结构'b- 因此self对该实现内部的任何引用也将自动具有生命周期'b。
fn match_str(&mut self, label: &'b str) {
Run Code Online (Sandbox Code Playgroud)
这里我们定义了match_str接受参数的方法label。label是一个字符串切片,它也有生命周期'b——所以它必须至少与self调用该方法的时间一样长。
在你的原始代码中,你有这样的东西:
impl Game<'_> {
fn match_str<'a>(&mut self, label: &'a str) {
...
}
}
Run Code Online (Sandbox Code Playgroud)
这是告诉编译器:
Game;这个结构有一个生命周期参数,但我们不关心它,我们不会把它与实现的任何元素联系起来match_str,并声明了一个生命周期'a,我们可以在函数签名的其余部分中引用它。label有生命周期的论点a,但我们没有将这个生命周期与其他任何事情联系起来更多信息:
| 归档时间: |
|
| 查看次数: |
1171 次 |
| 最近记录: |