Why does this
fn main() {
let test = "5% of foo".to_string();
let result: i32 = test.split('%').collect()[0].parse().unwrap_or(0);
}
cause an error
error[E0282]: type annotations needed
--> src/main.rs:4:23
|
4 | let result: i32 = test.split('%').collect()[0].parse().unwrap_or(0);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type for `B`
This doesn't help either:
let result: i32 = test.to_string().split('%').collect()[0].parse().unwrap_or(0i32);
It is also not specified what this iterator returns after the first None is returned. If you need fused iterator, use fuse. Creates an iterator that skips the first n elements. skip (n) skips elements until n elements are skipped or the end of the iterator is reached (whichever happens first). After that, all the remaining elements are yielded.
Because the closure passed to take_while () takes a reference, and many iterators iterate over references, this leads to a possibly confusing situation, where the type of the closure is a double reference: Stopping after an initial false:
Takes each element, adds them together, and returns the result. An empty iterator returns the zero value of the type. When calling sum () and a primitive integer type is being returned, this method will panic if the computation overflows and debug assertions are enabled.
If you need fused iterator, use fuse. Creates an iterator that skips the first n elements. skip (n) skips elements until n elements are skipped or the end of the iterator is reached (whichever happens first). After that, all the remaining elements are yielded.
fn main() {
let test = "5% of foo".to_string();
let result: i32 = test.split('%').collect::<Vec<_>>()[0].parse().unwrap_or(0);
}
collect()
can become any type that implements FromIterator
so a type hint is required.
Alternatively, you can make it more efficient by utilizing lazy iterators.
fn main() {
let test = "5% of foo".to_string();
let result: i32 = test.split('%').next().unwrap_or("0").parse().unwrap_or(0);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With