3

Given a 2D vector of i32s:

let v = vec![
    vec![1, 1, 1], 
    vec![0, 1, 0], 
    vec![0, 1, 0],
];

How can I pass it to a function to ultimately print its details? I tried:

fn printVector(vector: &[[i32]]) {
    println!("Length{}", vector.len())
}
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
camccar
  • 690
  • 12
  • 24
  • 1
    Check this question https://stackoverflow.com/questions/13102786/two-dimensional-vectors-in-rust – ozkriff Aug 15 '18 at 13:08
  • 2
    Idiomatic Rust uses `snake_case` for variables, methods, macros, and fields; `UpperCamelCase` for types; and `SCREAMING_SNAKE_CASE` for statics and constants. Use `print_vector` instead, please. – Shepmaster Aug 15 '18 at 13:20
  • 3
    See also [What type should I use for a 2-dimensional array?](https://stackoverflow.com/q/29523895/155423); [Creating a Vector of Vectors in Rust](https://stackoverflow.com/q/35630131/155423); [How can you easily borrow a Vec> as a &[&[T\]\]?](https://stackoverflow.com/q/50056778/155423). – Shepmaster Aug 15 '18 at 13:59
  • 4
    How do you want to pass `v` to the function? As value? As [reference](https://doc.rust-lang.org/std/primitive.reference.html)? Do you need the function to handle `Vec` or is a [slice](https://doc.rust-lang.org/std/slice/) enough? See *[What is the difference between passing a value to a function by reference and passing it by Box?](https://stackoverflow.com/questions/27305585)* – Tim Diekmann Aug 15 '18 at 14:14
  • 1
    It has nothing to do with offending eyes or something. This is the warning from the rust compiler: *warning: function `printVector` should have a snake case name such as `print_vector`* – Tim Diekmann Aug 15 '18 at 14:31
  • 1
    I'm aware of the compiler warnings. It's just a silly thing to complain about. Lots of helpful people gave me actual solutions rather than nit pick the question syntax. Those are the people who actually make stackover flow worth using. – camccar Aug 15 '18 at 15:48
  • Possible duplicate of [How can you easily borrow a Vec> as a &\[&\[T\]\]?](https://stackoverflow.com/questions/50056778/how-can-you-easily-borrow-a-vecvect-as-a-t) – mcarton Aug 15 '18 at 16:44
  • 3
    @camccar After his comment on the syntax, Shepmaster did your job and researched similar questions, and then gave you links to 3 different relevant questions. Those are the people who actually make stackoverflow worth using. – mcarton Aug 15 '18 at 16:49

4 Answers4

8

You may use a function which accepts a slice of T where T can also be referenced as a slice:

fn print_vector<T>(value: &[T])
where
    T: AsRef<[i32]>,
{
    for slice in value {
        println!("{:?}", slice.as_ref())
    }
}

playground

If you want to accept any type instead of just i32, you can also generalize this:

fn print_vector<T, D>(value: &[T])
where
    T: AsRef<[D]>,
    D: Debug,
{
    for slice in value {
        println!("{:?}", slice.as_ref())
    }
}

playground

Tim Diekmann
  • 7,755
  • 11
  • 41
  • 69
5

Since you're going to pass vectors to your function, the following code should work:

fn print_vector(vector: Vec<Vec<i32>>) {
    println!("Length{}", vector.len())
}
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Igor Drozdov
  • 14,690
  • 5
  • 37
  • 53
3

You need to pass a slice of vectors - &[Vec<i32>], not a slice of slices:

fn print_vector(vector: &[Vec<i32>]) {
    println!("Length {}", vector.len())
}

fn main() {
    let v = vec![vec![1, 1, 1], vec![0, 1, 0], vec![0, 1, 0]];
    print_vector(&v);
}

Playground

ozkriff
  • 1,269
  • 15
  • 23
1
fn printVector(vector: &Vec<Vec<i32>>) {
    println!("Length{}", vector.len())
}

let v = vec![
    vec![1, 1, 1],
    vec![0, 1, 0],
    vec![0, 1, 0],
];
printVector(&v);

In this example, &Vec<Vec<i32> and &[Vec<i32>] are no different; maybe you want to change to this:

fn print_vector(vector: &[Vec<i32>]) {
    for i in vector {
        for j in i {
            println!("{}", j)
        }
    }
}

let v = vec![
    vec![1, 1, 1],
    vec![0, 1, 0],
    vec![0, 1, 0],
];
print_vector(&v);
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
veekxt
  • 372
  • 1
  • 9