Two dimensional vectors in Rust

后端 未结 2 641
后悔当初
后悔当初 2020-12-20 14:18

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 v

相关标签:
2条回答
  • 2020-12-20 14:40

    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);
    }
    
    0 讨论(0)
  • 2020-12-20 14:50

    Did you intend that all of the subarrays will have the length 2, as in this example? In that case, the type of the parameter should not be &[u8], which is a borrowed array of u8's, but rather &[[u8; 2]].

    0 讨论(0)
提交回复
热议问题