Logo

Programming-Idioms

History of Idiom 26 > diff from v40 to v41

Edit summary for version 41 by programming-idioms.org:
[Rust] Use M and N. No sample values. No main func. +DemoURL

Version 40

2018-05-09, 20:32:38

Version 41

2018-05-10, 09:31:28

Idiom #26 Create a 2-dimensional array

Declare and initialize a matrix x having m rows and n columns, containing real numbers.

Illustration

Idiom #26 Create a 2-dimensional array

Declare and initialize a matrix x having m rows and n columns, containing real numbers.

Illustration
Code
fn main() {
  const M: usize = 4;
  const N: usize = 4;

  let x: [[f64; 4]; 4] = [[ 0.0,  1.0,  2.0,  3.0],
                          [ 4.0,  5.0,  6.0,  7.0],
                          [ 8.0,  9.0, 10.0, 11.0],
                          [12.0, 13.0, 14.0, 15.0]];

  assert_eq!(x[0][0], 0.0);
  assert_eq!(x[0][3], 3.0);
  assert_eq!(x[3][0], 12.0);

  println!("the value at 2,2 should be 10.0, {}", x[2][2]);
}
Code
let mut x = [[0.0; N] ; M];
Comments bubble
This works only when M and N are constant.
Elements have type f64
Demo URL
https://play.rust-lang.org/?gist=a25a0b7137c8f5c8d0534614b5996830&version=stable&mode=debug