When trying to run the following code:
fn main() {
let s1 = String::from("hello world");
println!("The first word in the string is: {}", return_first_word(&s1));
}
fn return_first_word(s: &String) -> String {
let t = s.split(" ");
String::from(t.next().unwrap())
}
I get the error cannot borrow as mutable on the line String::from(t.next().unwrap()). Naturally, when I replace the line let t = ... with let mut t = ... the code works perfectly.
My question is, can someone explain why? What is being borrowed here, and by what? I checked the type of my newly created immutable variable t and it is an iterator. Why would it be an issue iterating over an iterator??