3 ms·
The biggest difference between Scala and Rust here is ".iter()", which is necessary mainly because you need to specify whether the iterator iterates over refere
by pcwalton 10y ago
The biggest difference between Scala and Rust here is ".iter()", which is necessary mainly because you need to specify whether the iterator iterates over references (".iter()") or passes results by value (".into_iter()"). Because Java doesn't have value types and has no concept of move semantics, it has no need for the distinction, while Rust does.
In effect, the "elegance" of Scala here is really the product of its heavy GC dependence. So the comparison isn't as meaningful as it might seem.
- saosebastiao 10y agoWouldn't the type guarantees provided by rusts type system allow for different implementations based on whether it was called on value types vs reference types? I thought there was an auto specialization feature created a while ago that did something like that.
- foota 10y agoI think the into iterator version borrows the references (did I say that right?
- Tuna-Fish 10y agoIt allows changing implementation depending on what you pass it, but it cannot change how you pass things. The issue is that if you just call zip(code_lengths), then ownership of code_lengths is passed into the function and it ceases to work in it's parent.
- eddyb 10y agoThe example can be written like this though: let codes: Vec<HuffmanCode> = data_table.iter() .zip(&code_lengths) .zip(&code_table) .map(|((&value, &length), &code)| { HuffmanCode { length: length, code: code, value: value, } }) .collect();
- Veedrac 10y agoIn fact `code_lengths` and `code_table` are never used again so should just be passed by value, which saves four more `&`s.