637
you are viewing a single comment's thread
view the rest of the comments
[-] xor 15 points 2 years ago

You'll be happy to hear I've updated the example to be not bad

[-] Username@feddit.de 5 points 2 years ago* (last edited 2 years ago)

I wanted to ask why it's bad, what did you change?

Btw. the example function get_default is badly chosen, because unwrap_or_default exists.

[-] xor 11 points 2 years ago

The original example was doing the unwrap_within an iterator doing some string parsing, so there was a lot of unrelated boilerplate around the actual unwrapping that made it really unclear, as well as usual unwrap_or_else to produce a constant value

Ehhh, I was more using get_default as a placeholder for some function, as opposed to representing Default::default for the inner type specifically. I think it should be alright since only people familiar with rust would know about the default trait anyway. I did consider adding an unwrap_or_default example, but thought it was getting a bit off topic at that point.

[-] Username@feddit.de 4 points 2 years ago

Yeah I get it, it was just something I noticed. A pedantic lint, you could say.

[-] xor 1 points 2 years ago
[-] barsoap@lemm.ee 1 points 2 years ago

Maybe shouldn't call the function anything default and also provide it. The std docs say:

fn count(x: &str) -> usize { x.len() }

assert_eq!(Ok(2).unwrap_or_else(count), 2);
assert_eq!(Err("foo").unwrap_or_else(count), 3);
this post was submitted on 08 Dec 2023
637 points (100.0% liked)

Programmer Humor

37656 readers
133 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 6 years ago
MODERATORS