@Ambraven@social.mochi.academy
Post #2147726
2026-04-29 15:09 UTC
Replies (7)
-
@Ambraven@social.mochi.academy 2026-04-29 15:47
#rust #rustlang I can't "respond to all" so I'm answering here. So you gave me clean solution to do the same as my code, so thank you. All neat. However is there a way to not clone the A passed in the option ? It feels misleading to have a function take a reference and clone the data under the hood.
-
@emily_s@mastodon.me.uk 2026-04-29 15:12
@Ambraven@social.mochi.academy Something like: `foo.map(|f| f.clone()).unwrap_or_else(||A::new())`
-
@mohs@climatejustice.social 2026-04-29 15:15
@Ambraven@social.mochi.academy depending on what you want to achieve you have different options. If A implements default you can use unwrap_or_default(). You can first clone foo, as you are cloning inside anyway. Having a ref in an option is pretty common, so you should force yourself into cloning / rc'ing.
-
@B3NNY@infosec.exchange 2026-04-29 15:17
@Ambraven@social.mochi.academy `let foo = foo.map(Clone::clone).unwrap_or_else(|| A::new())`
-
@ebel@moytura.org 2026-04-29 15:20
@Ambraven@social.mochi.academy how about `Option::map_or` / `Option::map_or_else` ? https://doc.rust-lang.org/std/option/enum.Option.html#method.map_or_else
-
@nik@toot.teckids.org 2026-04-29 15:23
@Ambraven@social.mochi.academy foo.cloned().unwrap_or() https://doc.rust-lang.org/std/option/enum.Option.html#method.cloned
-
@Hemera@meow.social 2026-04-29 15:27
@Ambraven@social.mochi.academy I'll add to the bandwagon by going shorter: foo.cloned().unwrap_or(A::new)