I defined a recursive function that creates a tree-like structure from another. The value created inside the function can't get out.
I've tried different ways of getting ownership out of the function, returning the value itself, passing a vector as argument to store the value.
#[derive(Clone, Copy)]
pub enum Concept<'a> {
Variable,
Base(&'a str),
ForAll(&'a str, &'a Concept<'a>),
}
pub fn normal_form_reduction<'a>(
con: &Concept<'a>,
container: &mut Vec<Concept<'a>>,
) -> Concept<'a> {
match *con {
// bases cases without recurrence
Concept::Variable => Concept::Variable,
Concept::Base(s) => Concept::Base(&*s),
//...
//first recursive call
Concept::ForAll(s, c) => {
let nc = normal_form_reduction(c, container);
(*container).push(nc);
Concept::ForAll(s, &nc)
}
}
}
Expected to compile. Get:
error[E0515]: cannot return value referencing local variable `nc`
--> src/lib.rs:22:13
|
22 | Concept::ForAll(s, &nc)
| ^^^^^^^^^^^^^^^^^^^---^
| | |
| | `nc` is borrowed here
| returns a value referencing data owned by the current function
The error 'E0515' don't explain my error.