I have a function which takes a reference and a condition. If the condition is false, it uses the passed in reference, but when the condition is true, it use a temp reference pointing to the local variable in the function.
The Foo
struct is NOT Copy
or Clone
.
The commented-off code won't compile and I have an ugly way to work around this problem:
#[derive(Debug)]
struct Foo {
a: String,
}
fn f(r: &Foo, cond: bool) {
// let a = if cond { &Foo { a: "456".into() } } else { r };
// use a temp variable `t` can solve this problem in this demo code,
// but what if in production code where build tmp var `t` is expensive?
let mut a = r;
let t = &Foo { a: "456".into() };
if cond {
a = &t;
}
// use the ref here, inside the function.
println!("{:#?}", a);
}
fn main() {
let a = Foo { a: "123".into() };
f(&a, true);
}
Because the use of the reference is limited in the function, is there a way to tell the compiler: do not free the temp var created in the if branch, keep it until the function return and destroy it when destroy the stack frame?
I think there should be a way without using heap memory. On the other hand, when using Box
, the type produced in the two if branch will be different, one is &Foo
and another is Box<Foo>
, so this won't be the right way.