最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

rust - Return value with lifetime bound with struct's field - Stack Overflow

programmeradmin3浏览0评论

Cannot return value from a HashMap nested in a struct.

Given rust code as

pub struct State {
    m: HashMap<i32, String>,
}

pub struct X<'a> {
    state: &'a mut State,
}

impl<'a> X<'a> {
    pub fn get(&mut self, k: i32) -> Option<&'a String> {
        if let Some(v) = self.state.m.get(&k) {
            return Some(v);
        }

        None
    }
}

It broke with error indicating

error: lifetime may not live long enough
  --> src/main.rs:18:20
   |
15 | impl<'a> X<'a> {
   |      -- lifetime `'a` defined here
16 |     pub fn get(&mut self, k: i32) -> Option<&'a String> {
   |                - let's call the lifetime of this reference `'1`
17 |         if let Some(v) = self.state.m.get(&k) {
18 |             return Some(v);
   |                    ^^^^^^^ method was supposed to return data with lifetime `'a` but it is returning data with lifetime `'1`

error: could not compile `hello-world-rs` (bin "hello-world-rs") due to 1 previous error

How to fix it?

Cannot return value from a HashMap nested in a struct.

Given rust code as

pub struct State {
    m: HashMap<i32, String>,
}

pub struct X<'a> {
    state: &'a mut State,
}

impl<'a> X<'a> {
    pub fn get(&mut self, k: i32) -> Option<&'a String> {
        if let Some(v) = self.state.m.get(&k) {
            return Some(v);
        }

        None
    }
}

It broke with error indicating

error: lifetime may not live long enough
  --> src/main.rs:18:20
   |
15 | impl<'a> X<'a> {
   |      -- lifetime `'a` defined here
16 |     pub fn get(&mut self, k: i32) -> Option<&'a String> {
   |                - let's call the lifetime of this reference `'1`
17 |         if let Some(v) = self.state.m.get(&k) {
18 |             return Some(v);
   |                    ^^^^^^^ method was supposed to return data with lifetime `'a` but it is returning data with lifetime `'1`

error: could not compile `hello-world-rs` (bin "hello-world-rs") due to 1 previous error

How to fix it?

Share Improve this question edited Mar 11 at 4:32 John Kugelman 363k69 gold badges553 silver badges597 bronze badges asked Mar 10 at 9:27 sammynesammyne 1216 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

You have not bound &mut self to the same lifetime 'a, so Rust compiler has to assume they are different lifetimes, which leads to your lifetime mismatch.

Also note that (thanks @kmdreko) 'a here as it is written refers to the lifetime associated with the type (X<'a>), which is distinct from the lifetime of the reference to self (&mut self). Therefore it would be inappropriate to use it here.

To fix it, simply change your function signature:

pub fn get<'b>(&'b mut self, k: i32) -> Option<&'b String>

Note that we introduced a new lifetime 'b to express the fact that it's distinct from 'a.

P.s. in fact, due to Rust's lifetime elision rules, this will work as well:

pub fn get(&mut self, k: i32) -> Option<&String>

You may also want to replace 'a with an anonymous lifetime (impl<'a> X<'a> -> impl X<'_>), because it's not used anywhere else.

发布评论

评论列表(0)

  1. 暂无评论