What I am trying to do
I have in iterator returned from std::str::SplitWhitespace
, I need the first element, and a vector of all elements.
What I have tried
I tried to use peek. However this seems to need a mutable (I can't we why), and I end up with borrowing errors.
Simplified code, with compile errors.
fn main(){
let line = "hello, world";
let mut tokens = line.split_whitespace().peekable();
if let Some(first) = tokens.peek() {
//println!("{first}"); //works
//println!("{tokens:?}"); // works
println!("{first}\n{tokens:?}"); //compile error
}
}
error[E0502]: cannot borrow `tokens` as immutable because it is also borrowed as mutable
--> src/main.rs:7:29
|
4 | if let Some(first) = tokens.peek() {
| ------------- mutable borrow occurs here
...
7 | println!("{first}\n{tokens:?}"); //error
| --------------------^^^^^^-----
| | |
| | immutable borrow occurs here
| mutable borrow later used here
If I un-comment the two println
s, and comment the erroneous one. then It works. This lead me to adding a clone let first = first.clone();
, before the println
s. This fixed it, but I am wondering if there is a better way.