3 ms·
I like matching on bools on the same cases where someone wouldn't write braces around the body in C: whenever the corresponding expressions keep you under the l
by estebank 5y ago
I like matching on bools on the same cases where someone wouldn't write braces around the body in C: whenever the corresponding expressions keep you under the line length. For example:
let plural = match cases.len() > 1 {
true => "s",
false => "",
};
Of course, you'd more likely have something like
let prefix = match cases.len() {
0 => "no values".to_string(),
1 => "a value".to_string(),
n => format!("{} values", n),
};
- iudqnolq 5y agoInteresting. I prefer writing that as an if/else on one line when rufmt lets me, but I see your point.
- rabidferret 5y agolet plural = match cases.len() { 1.. => "s", 0 => "", }; ;P
- cyphar 5y agoThat's not the correct plural rule in English, you'd want let plural = match cases.len() { 0 | 2.. => "s", 1 => "", }; or something (though to be fair the original code snippet also handled "0" incorrectly -- should be "cases.len() != 1").
- iudqnolq 5y agoAnd also to be fair you probably don't want to be hardcoding English pluralization rules.
- rabidferret 5y agoI used to maintain a library which actually tried to implement proper English pluralization rules. I can confirm you don't want to do that.
- spullara 5y agoMy favorite thing about internationalization is that some languages have 6 different translations for different amounts.