Pass mutable reference to vector as argument


#1

Hello there !

I’m reading borrowing chapter from the rust book.
In the previous example it is explained that passing a “simple” reference to a vector do not allow the function to mute it.
So, in the next section, mutables references are introduced, so I’m trying to apply this concept with the same example.
Here is my code:

fn take_ownership(v1: &mut Vec<i32>){
    v1.push(42);
}

fn main() {
    let mut v1 = vec![1, 2, 3];
    take_ownership(&mut v1);
}

But when I’m trying to compile this, I get this error:

   Compiling hello_world v0.1.0 (/home/matthieu/Config/settings/Projets/Rust/hello_world)
     Running `rustc --crate-name hello_world src/main.rs --color always --crate-type bin --emit=dep-info,link -C debuginfo=2 -C metadata=62e4bdd6224c686e -C extra-filename=-62e4bdd6224c686e --out-dir /home/matthieu/Config/settings/Projets/Rust/hello_world/target/debug/deps -C incremental=/home/matthieu/Config/settings/Projets/Rust/hello_world/target/debug/incremental -L dependency=/home/matthieu/Config/settings/Projets/Rust/hello_world/target/debug/deps`
error: Could not compile `hello_world`.                                                                                                                                                                                                        

Caused by:
  process didn't exit successfully: `rustc --crate-name hello_world src/main.rs --color always --crate-type bin --emit=dep-info,link -C debuginfo=2 -C metadata=62e4bdd6224c686e -C extra-filename=-62e4bdd6224c686e --out-dir /home/matthieu/Config/settings/Projets/Rust/hello_world/target/debug/deps -C incremental=/home/matthieu/Config/settings/Projets/Rust/hello_world/target/debug/incremental -L dependency=/home/matthieu/Config/settings/Projets/Rust/hello_world/target/debug/deps` (signal: 11, SIGSEGV: invalid memory reference)

What I am doing wrong ?


#2

Hmm, it seems that your code is fine, just that the compiler for some reason decided to segfault… How unfortunate…


#3

Oh and also, you call

fn take_ownership(v1: &mut Vec<i32>){
    v1.push(42);
}

Take ownership, but in actuality,

fn take_ownership(v1: Vec<i32>){
    v1.push(42);
}

Is what would take ownership, the first one just takes ownership of the reference, and not the value which it points to.


#4

I tried it on the playground with a bunch of different combinations of release/debug and stable/beta/nightly and they all worked fine.


#5

Perhaps a restart? Or if that doesn’t work, try the following and compile between:

  1. rustup toolchain install stable and then rustup default stable
  2. rustup toolchain install nightly and then rustup default nightly
  3. rustup self update and repeat 1 & 2
  4. rustup self uninstall and reinstall rustup and then try 1 & 2

#6

I’m so sorry, I didn’t specified my environment…
I’m running rustc 1.30.0+dfsg1-2 and cargo 0.31.0-2 from the debian sid’s repos.
Is the Rust script the recommended way to install rustc and Cargo ?


#7

All right. I removed debian packages and then installed via the Rust shell script and everything is fine now.
Sorry for the noise, and thank you !