I have a struct that contains references to two different values. There's two constructors - one that takes a reference for each value, and one that only takes a reference to one of the values while assigning a default to the other.
My problem is assigning that default. See the code below:
struct Foo<'t> {
a: &'t String,
b: &'t String,
}
impl<'t> Foo<'t> {
fn new(a: &'t String, b: &'t String) -> Foo<'t> {
Foo { a, b }
}
fn new_with_default_b(a: &'t String) -> Foo<'t> {
Foo {
a,
b: &String::from("default"),
}
}
}
This does not compile:
error[E0515]: cannot return value referencing temporary value
--> src/lib.rs:12:9
|
12 | / Foo {
13 | | a,
14 | | b: &String::from("default"),
| | ----------------------- temporary value created here
15 | | }
| |_________^ returns a value referencing data owned by the current function
Is it possible to fix this?