sirjana luitel
sirjana luitel

Reputation: 43

How to pass a vector as a parameter in rust

I am trying to write a function that prints a vector. I am having some trouble in understanding how to pass a vector as a parameter. This is what I have so far:

fn vecprinter(v1: &mut Vec<u32>) -> Vec<u32> {

v1
}

fn main(){

  let mut v1=vec![1,10,11,12,13];
  
  println!("{:?}", vecprinter(v1));
}

However I am getting this error:

error[E0308]: mismatched types
 --> main.rs:3:1
  |
1 | fn vecprinter(v1: &mut Vec<u32>) -> Vec<u32> {
  |                                     -------- expected `std::vec::Vec<u32>` because of return type
2 | 
3 | v1
  | ^^
  | |
  | expected struct `std::vec::Vec`, found mutable reference
  | help: try using a conversion method: `v1.to_vec()`
  |
  = note:         expected struct `std::vec::Vec<u32>`
          found mutable reference `&mut std::vec::Vec<u32>`

error[E0308]: mismatched types
  --> main.rs:10:31
   |
10 |   println!("{:?}", vecprinter(v1));
   |                               ^^
   |                               |
   |                               expected mutable reference, found struct `std::vec::Vec`
   |                               help: consider mutably borrowing here: `&mut v1`
   |
   = note: expected mutable reference `&mut std::vec::Vec<u32>`
                         found struct `std::vec::Vec<{integer}>`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0308`.
compiler exit status 1

What am I doing wrong?

Upvotes: 0

Views: 5218

Answers (2)

yolenoyer
yolenoyer

Reputation: 9465

You wrote in your question:

I am trying to write a function that prints a vector.

But the function you wrote (no matter if it works or not) is not writing anything: it returns the given vector.

A function which would print something would look like this:

fn print_something(to_print: /*Some type*/) /*No return type*/ {
    println!(/*...*/);
}

As you want to print a vector of u32, the prototype could be:

fn print_something(to_print: Vec<u32>) { /*...*/ }

But if you do that, the argument will be moved into the function and will become unusable outside of it. In order to improve this, you can pass the argument as a reference:

fn print_something(to_print: &Vec<u32>) { /*...*/ }

To handle more general cases (&Vec<u32> + &[u32]), you can still improve it like this:

fn print_something(to_print: &[u32]) { /*...*/ }

Now when you call this function, you have to add an ampersamp & to the given argument (unless it's already a reference):

print_something(&my_vector);

Finally, by gluing everything together, you have this:

fn print_vec(v: &[u32]) {
    println!("{:?}", v);
}

fn main() {
    let v = vec![1, 10, 11, 12, 13];
    print_vec(&v);

    // Because we changed "&Vec<u32>" to "&[u32]", the following is also
    // possible:
    let arr = &[1, 10, 11, 12, 13];
    print_vec(arr);
}

Upvotes: 1

joshmeranda
joshmeranda

Reputation: 3261

You have two issues here.

Firs your vecprinter method takes a &mut Vec<u32 as a parameter, but you are passing it a Vec<u32> in your call to println!. To resolve this pass a mutable reference t to the vec instead:

println!("{:?}", vecprinter(&mut v1));

The second issue is with the return types of the vecprinter method. You are taking in a &mut Vec<u32> and expecting to return a Vec<u32>; however, when you return the value of v1 you are returning a &mut Vec<u32> rather than the expected Vec<u32>. Depending on your requirements you have two options.

Change the return type of the vecprinter method and pass a mutable reference:

fn vecprinter(v1: &mut Vec<u32>) -> &mut Vec<u32> {
    v1
}

fn main(){

  let mut v1=vec![1,10,11,12,13];
  
  println!("{:?}", vecprinter(&mut v1));
}

or change the parameter value to take the vector rather than a reference:

fn vecprinter(v1: Vec<u32>) -> Vec<u32> {
    v1
}

fn main(){

  let mut v1=vec![1,10,11,12,13];
  
  println!("{:?}", vecprinter(v1));
}

Upvotes: 3

Related Questions