Hi everyone,
Im completely new to Rust and trying to get my head around references and the ownership/borrowing model.
i’m doing an small example that in one way works but not in the other, could you explain me why ?
This code does not work:
“Can not borrow expr_chars as mutable more than once at a time”
let expr = String::from("2+1+3");
let mut expr_chars = expr.chars().peekable();
let fchar = expr_chars.next();
let pfchar = expr_chars.peek();
let schar = expr_chars.next();
let pschar = expr_chars.peek();
println!("{:?}", fchar);
println!("{:?}", pfchar);
println!("{:?}", schar);
println!("{:?}", pschar);
but this one does
let expr = String::from("2+1+3");
let mut expr_chars = expr.chars().peekable();
println!("{:?}", expr_chars.next());
println!("{:?}", expr_chars.peek());
println!("{:?}", expr_chars.next());
println!("{:?}", expr_chars.peek());
as expected it prints 2 , +, +, 1