3 ms·
I don't know. I find this: fn get_ids(data: Vec<Widget>) -> Vec<Id> { let mut result = Vec::new(); for widget in &data { i
by EnPissant 1y ago
I don't know. I find this:
fn get_ids(data: Vec<Widget>) -> Vec<Id> {
let mut result = Vec::new();
for widget in &data {
if widget.alive {
result.push(widget.id);
}
}
result
}
more readable than this:
fn get_ids(data: Vec<Widget>) -> Vec<Id> {
data.iter()
.filter(|w| w.alive)
.map(|w| w.id)
.collect()
}
and I also dislike Rust requiring you to write "mut" for function mutable values. It's mostly just busywork and dogma.
- Mond_ 1y agoYeah, I really wanted to avoid a discussion over functional vs. imperative programming, so I just... didn't talk about the imperative style at all, and just said so in the first section. I think the imperative style isn't as readable (of course I would), but that's absolutely a discussion for another day, and I get why people prefer it.
- EnPissant 1y agoI think it’s important to point out what the imperative version would look like because I think the fundamental reason that the method chaining approach is more readable is because it more closely resembles imperative code. When reading it, you start with a vector and then you mutate it in various ways before returning it. I understand that’s not how it’s implemented under the hood, but I don’t think it really matters.