Elektrine lite

← Feed

@benjamingeer@piaille.fr

Post #2564790

2026-04-29 16:04 UTC

@Ambraven@social.mochi.academy The problem is that if the Option is empty, you're making a new instance of A. If you take a reference to that, the A still has to stay in scope (i.e. not be dropped) while you're using the reference. You could do something like this: fn use_f(f: &A) { println!("{f:?}"); } fn toto(foo: Option) { if let Some(f) = foo { use_f(f); } else { let f = A::new(); use_f(&f); } }

Replies (1)

  • @benjamingeer@piaille.fr ... Oh I know, I can do this: fn toto(foo: Option ) { if foo.is_none() { toto(Some(A::new()); return; } let foo = foo.unwrap(); ... } It might become a bit messy if there are more than one optional parameter though...

    Open ##2564791