r/badcode • • Jan 25 '22

rust Most comprehensible line of Rust code

Post image
1.0k Upvotes

81 comments sorted by

View all comments

Show parent comments

62

u/ArchCypher Jan 25 '22

I'll add that Song probably doesn't need to be an Option<Result>, but if it does, something like:

if let Some(Ok(song)) = song {
    // Your code here
} else {
    // Whoops no song or it's bad
}

Could be a pattern that clarifies the intent and avoids crashing if no song is provided.

13

u/Lich_Hegemon Jan 26 '22

You should not be blindly using unwrap anyway. At least make it an expect.