Reputation: 167
In the following snippet of code, Rust complains that the return types do not match.
let line = String::new(); // assume string has content
let line: String = line.bytes()
.flat_map(|b| {
if b > 0x7F {
format!("M-{}", char::from(b - 0x7F)).bytes()
}
else {
[b].into_iter() // error happens here
}
})
.map(|b| { char::from(b) })
.collect();
The error is the following:
if
andelse
have incompatible types
expected typestd::str::Bytes<'_>
found structstd::slice::Iter<'_, u8>
How can I avoid this?
Upvotes: 1
Views: 550
Reputation: 167
I got the answer here.
The easiest solution is to use the either crate.
Upvotes: 0