Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR RUST

rust Clean way to get Option::unwrap_or_else behaviour with an Option<&T>

fn use_or_default_nicer(thing_ref_opt: Option<&ExpensiveUnclonableThing>) {
    let mut maybe = None;
    let thing_ref = thing_ref_opt.unwrap_or_else(
        || maybe.insert(make_the_thing())
    );
    use_the_thing(thing_ref);
}
 
PREVIOUS NEXT
Tagged: #rust #Clean #behaviour
ADD COMMENT
Topic
Name
9+5 =