I'm new to rust and I'm having some issues that I can't seem to figure out on my own.
The code:
struct Value;
enum Val<'a> {
Null,
String(&'a str),
Symbol(&'a str),
Builtin(Box<dyn Fn(Vec<&Val>) -> Val<'a>>),
Function(Vec<&'a str>, &'a ()),
}
struct Scope<'a> {
idents: std::collections::HashMap<&'a str, Val<'a>>,
parent: Option<&'a Scope<'a>>,
}
fn apply<'a>(scope: &'a Scope<'a>) -> Val<'a> {
let mut _new_scope = Scope{
idents: std::collections::HashMap::new(),
parent: Some(scope),
};
Val::Null
}
fn eval<'a>(_val: &'a Value, scope: &'a Scope<'a>) -> Val<'a> {
apply(scope);
Val::Null
}
fn main() {
let v = Value;
let root_scope = Scope {
idents: std::collections::HashMap::new(),
parent: None,
};
let _ = eval(&v, &root_scope);
}
I get the following error:
error[E0597]: `root_scope` does not live long enough
--> src/main.rs:37:22
|
37 | let _ = eval(&v, &root_scope);
| ^^^^^^^^^^^ borrowed value does not live long enough
38 | }
| -
| |
| `root_scope` dropped here while still borrowed
| borrow might be used here, when `root_scope` is dropped and runs the destructor for type `Scope<'_>`
I've tried removing the explicit lifetime on scope in eval, apply, and the Scope struct, but then I get this error:
error[E0106]: missing lifetime specifier
--> src/main.rs:13:20
|
13 | parent: Option<&Scope<'a>>,
| ^ expected named lifetime parameter
|
help: consider using the `'a` lifetime
|
13 | parent: Option<&'a Scope<'a>>,
| ^^^
Any help is appreciated.