如何使用结构和隐式生命周期来推断实现的适当生命周期?

问题描述 投票:1回答:1

如何解决此错误?当我在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
    });
}

当我尝试编译时,出现以下错误:

error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
  --> src/main.rs:18:22
   |
18 |         let mut lh = LineHandlerInfo {
   |                      ^^^^^^^^^^^^^^^
   |
note: first, the lifetime cannot outlive the lifetime 'a as defined on the method body at 12:18...
  --> src/main.rs:12:18
   |
12 |     fn match_str<'a>(
   |                  ^^
note: ...so that reference does not outlive borrowed content
  --> src/main.rs:19:13
   |
19 |             label,
   |             ^^^^^
note: but, the lifetime must be valid for the lifetime '_ as defined on the impl at 11:11...
  --> src/main.rs:11:11
   |
11 | impl Game<'_> {
   |           ^^
   = note: ...so that the expression is assignable:
           expected LineHandlerInfo<'_>
              found LineHandlerInfo<'_>

我已经能够制作出较小的情况来再现错误。我以为我命名了所有生命周期,但是由于编译器仍然报告匿名生命周期,因此我必须缺少一些内容

struct LineHandlerInfo <'a> {
    label: &'a str,
}

struct Game<'b> {
    handlers: Vec<LineHandlerInfo<'b>>,
}

impl<'c> Game<'c> {
    fn do_something<'d>(
        &'d mut self,
        label: &'d str
    ) {
        let lh = LineHandlerInfo {
            label,
        };
        self.handlers.push(lh);
    }
}

fn main() {
    let mut g = Game {
        handlers: Vec::new(),
    };
    g.do_something("hello");
}

错误:

Compiling playground v0.0.1 (/playground)
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
  --> src/main.rs:14:18
   |
14 |         let lh = LineHandlerInfo {
   |                  ^^^^^^^^^^^^^^^
   |
note: first, the lifetime cannot outlive the lifetime 'd as defined on the method body at 10:21...
  --> src/main.rs:10:21
   |
10 |     fn do_something<'d>(
   |                     ^^
note: ...so that reference does not outlive borrowed content
  --> src/main.rs:15:13
   |
15 |             label,
   |             ^^^^^
note: but, the lifetime must be valid for the lifetime 'c as defined on the impl at 9:6...
  --> src/main.rs:9:6
   |
9  | impl<'c> Game<'c> {
   |      ^^
   = note: ...so that the expression is assignable:
           expected LineHandlerInfo<'c>
              found LineHandlerInfo<'_>

Playground link

<<

我该如何解决此错误?

从函数中删除通用生存期,在impl块上提供生存期的名称,而不是使用匿名生存期,然后在函数参数中使用命名的生存期。从&self中删除生命周期:

impl<'a> Game<'a> { fn match_str(&mut self, label: &'a str, match_literal: &'a str, f: fn(&str) -> Option<&str>) { self.handlers.push(LineHandlerInfo { label, match_literal, f, }); } }

另请参见:

另请参见:

    版本指南中的['_, the anonymous lifetime
  • 对于其中具有函数指针的结构
  • 这与函数指针无关。编程时遇到问题时,建议创建一个'_,剥离出不会使错误消失的内容。这使您可以专注于眼前的问题。例如,这会重现相同的错误:

minimal, reproducible example

rust lifetime
1个回答
0
投票
我该如何解决此错误?
© www.soinside.com 2019 - 2024. All rights reserved.