#![allow(unused)]
fn main() {
let arr = vec![(String::from("1"))];
let iterator = arr.iter();
let keyword = &iterator.find(|(&keyword)| keyword.eq("1")); // a
println!("{:?}", keyword)
}
I have two questions:
First question:
Error occurs in the line with flag a, I am wonder why it should change to mut
. Intuitively, only if I want to mutate the found element, I should decorate arr
with mut
and use arr.iter_mut
.
Second question:
I try to fix it, When I use arr.iter()
instead of &iterator
, the error gone:
-let keyword = &iterator.find(|(&keyword)| keyword.eq("1"));
+let keyword = arr.iter().find(|(&keyword)| keyword.eq("1"));
But if I not inline arr.iter()
, the error back again:
-let keyword = &iterator.find(|(&keyword)| keyword.eq("1"));
+let iterator = arr.iter();
+let keyword = iterator.find(|(&keyword)| keyword.eq("1"));
Are there some differences between inline it or not inline it?
Errors:
Compiling playground v0.0.1 (/playground)
error[E0596]: cannot borrow `iterator` as mutable, as it is not declared as mutable
--> src/main.rs:5:20
|
4 | let iterator = arr.iter();
| -------- help: consider changing this to be mutable: `mut iterator`
5 | let keyword = &iterator.find(|(&keyword)| keyword.eq("1"));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot borrow as mutable
For more information about this error, try `rustc --explain E0596`.
error: could not compile `playground` due to previous error