Since 1.51, Rust has included reduce, which I'm used to from Scala.
fold works like foldLeft in Scala, but reduce is different. What am I getting wrong?
This works beautifully:
let ss = vec!["a", "b", "c"].iter()
.fold("".to_string(), |cur, nxt| cur + nxt);
println!("{}", ss);
This does not:
let ss = vec!["a", "b", "c"].iter()
.reduce(|cur, nxt| cur + nxt);
println!("{}", ss);
Errors:
error[E0369]: cannot add `&&str` to `&&str`
--> src/main.rs:3:28
|
3 | .reduce(|cur, nxt| cur + nxt);
| --- ^ --- &&str
| |
| &&str
error[E0277]: `Option<&&str>` doesn't implement `std::fmt::Display`
--> src/main.rs:5:20
|
5 | println!("{}", ss);
| ^^ `Option<&&str>` cannot be formatted with the default formatter
|
= help: the trait `std::fmt::Display` is not implemented for `Option<&&str>`
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
= note: required by `std::fmt::Display::fmt`
= note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info)
What am I doing wrong?