Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two dimensional vectors in Rust

Editor's note: This question predates Rust 0.1 (tagged 2013-07-03) and is not syntactically valid Rust 1.0 code. Answers may still contain valuable information.

Does anyone know how to create mutable two-dimensional vectors in Rust and pass them to a function to be manipulated?

This is what I tried so far:

extern crate std;

fn promeni(rec: &[u8]) {
    rec[0][1] = 0x01u8;
}

fn main() {
    let mut rec = ~[[0x00u8,0x00u8],
        [0x00u8,0x00u8]
    ];
    io::println(u8::str(rec[0][1]));
    promeni(rec);
    io::println(u8::str(rec[0][1]));
}
like image 268
php-- Avatar asked Oct 27 '12 18:10

php--


1 Answers

You could use the macro vec! to create 2d vectors.

fn test(vec: &mut Vec<Vec<char>>){
    vec[0][0] = 'd';
    ..//
    vec[23][79] = 'd';
}

fn main() {

    let mut vec = vec![vec!['#'; 80]; 24];

    test(&mut vec);
}
like image 155
Angel Angel Avatar answered Oct 03 '22 00:10

Angel Angel