There is an existing value a
, and I want to get a reference to either a
or a new value b
created on demand, depending on some condition. The code below won't compile. I would like to know what is the idiomatic way of doing so in Rust.
fn main() { let condition = false; let a: String = "a".to_string(); let r: &String = if condition {&a } else { let b: String = "b".to_string();&b };}
New example (in response to @PitaJ):
struct S(i32);fn main() { let condition = false; let a: S = S(0); let r: &S = if condition {&a } else { let b: S = S(1);&b };}