Last active
March 12, 2024 12:24
-
-
Save timmc/1346e695afbe2ba179543a6249d337aa to your computer and use it in GitHub Desktop.
cannot borrow `stream` as mutable, as it is not declared as mutable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn outer(data: &mut Vec<u32>) { // unexpected error on this line | |
inner(&mut data); | |
inner(&mut data); | |
} | |
fn inner(data: &mut Vec<u32>) {} | |
// error[E0596]: cannot borrow `data` as mutable, as it is not declared as mutable | |
// --> src/example.rs:1:10 | |
// | | |
// 1 | fn outer(data: &mut Vec<u32>) { | |
// | ^^^^ not mutable | |
// 2 | inner(&mut data); | |
// | --------- cannot borrow as mutable | |
// 3 | inner(&mut data); | |
// | --------- cannot borrow as mutable | |
// | | |
// note: the binding is already a mutable borrow | |
// --> src/example.rs:1:16 | |
// | | |
// 1 | fn outer(data: &mut Vec<u32>) { | |
// | ^^^^^^^^^^^^^ | |
// help: try removing `&mut` here | |
// | | |
// 2 - inner(&mut data); | |
// 2 + inner(data); | |
// | | |
// help: try removing `&mut` here | |
// | | |
// 3 - inner(&mut data); | |
// 3 + inner(data); | |
// | | |
// ====================================================================== | |
// However, changing either `inner(&mut data);` to `inner(data);` clears the error on the method's parameter: | |
fn outer(data: &mut Vec<u32>) { // no longer an error here | |
inner(data); // line changed | |
inner(&mut data); | |
} | |
fn inner(data: &mut Vec<u32>) {} | |
// error[E0596]: cannot borrow `data` as mutable, as it is not declared as mutable | |
// --> src/example.rs:3:11 | |
// | | |
// 3 | inner(&mut data); | |
// | ^^^^^^^^^ cannot borrow as mutable | |
// | | |
// note: the binding is already a mutable borrow | |
// --> src/example.rs:1:16 | |
// | | |
// 1 | fn outer(data: &mut Vec<u32>) { | |
// | ^^^^^^^^^^^^^ | |
// help: try removing `&mut` here | |
// | | |
// 3 - inner(&mut data); | |
// 3 + inner(data); | |
// | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment