Skip to content

Instantly share code, notes, and snippets.

@timmc
Last active March 12, 2024 12:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timmc/1346e695afbe2ba179543a6249d337aa to your computer and use it in GitHub Desktop.
Save timmc/1346e695afbe2ba179543a6249d337aa to your computer and use it in GitHub Desktop.
cannot borrow `stream` as mutable, as it is not declared as mutable
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