Function that returns a value in a HashMap using a parameter requires a lifetime for Struct 'a but

preview_player
Показать описание

Below, you can find the text related to the question/problem. In the video, the question will be presented first, followed by the answers. If the video moves too fast, feel free to pause and review the answers. If you need more detailed information, you can find the necessary sources and links at the bottom of this description. I hope this video has been helpful, and even if it doesn't directly solve your problem, it will guide you to the source of the solution. I'd appreciate it if you like the video and subscribe to my channel!Function that returns a value in a HashMap using a parameter requires a lifetime for Struct 'a but not for &str

To provide some background, I am writing a parser that creates objects that reference the text from some &'a str, where 'a ties all objects to the lifetime of that string. While parsing, I do some lookups to get previously-parsed items by name, which are stored in a HashMap that holds objects that are tied to the lifetime of the parsed string.
&'a str
'a
HashMap
Previously, I was using a &str as the identifier, which worked. Here's a simplified example of what I had previously, which successfully compiled:
&str
use std::collections::HashMap;

struct ReturnType 'a {
key: &'a str,
value: &'a str
}

struct App 'a {
// Key type is &'a str
map: HashMap &'a str, &'a str ,
}

impl 'a App 'a {
// Note that I don't have to specify the lifetime of `key` because it's only used for hashing;
// the return type does not (and shouldn't) capture the lifetime of `key`, since it's returning
pub fn lookup_value(&self, key: &str) - Option ReturnType 'a {
}
}

fn lookup 'a (app: &'a App) - Option ReturnType 'a {
// Note that I'm calling lookup_value referencing a value with a lifetime shorter than 'a
let key = String::from("te");
}

fn main() {
let root = "testing";

let app = App {
map: HashMap::from([(&root[0..2], root)]),
};

lookup(&app);
}

use std::collections::HashMap;

struct ReturnType 'a {
key: &'a str,
value: &'a str
}

struct App 'a {
// Key type is &'a str
map: HashMap &'a str, &'a str ,
}

impl 'a App 'a {
// Note that I don't have to specify the lifetime of `key` because it's only used for hashing;
// the return type does not (and shouldn't) capture the lifetime of `key`, since it's returning
pub fn lookup_value(&self, key: &str) - Option ReturnType 'a {
}
}

fn lookup 'a (app: &'a App) - Option ReturnType 'a {
// Note that I'm calling lookup_value referencing a value with a lifetime shorter than 'a
let key = String::from("te");
}

fn main() {
let root = "testing";

let app = App {
map: HashMap::from([(&root[0..2], root)]),
};

lookup(&app);
}

However, I recently replaced &'a str in the HashMap key with a new wrapper type, StrRef 'a which simply wraps around the same &'a str that was previously used. However, now it doesn't compile:
&'a str
HashMap
StrRef 'a
&'a str
use std::collections::HashMap;

#[derive(Clone, Hash, PartialEq, Eq)]
struct StrRef 'a {
value: &'a str,
}

struct ReturnType 'a {
key: StrRef 'a ,
value: &'a str
}

struct App 'a {
// Key type is StrRef 'a
map: HashMap StrRef 'a , &'a str ,
}

impl 'a App 'a {
// I have tried `StrRef` and `StrRef 'b ` and all have the same error
pub fn lookup_value(&self, key: &StrRef) - Option ReturnType 'a {
// Error: explicit lifetime required in the type of `key`; lifetime `'a` required
}
}

fn lookup 'a (app: &'a App) - Option ReturnType 'a {
let key = String::from("te");
Source of the question:

Question and source license information:
Рекомендации по теме
join shbcf.ru