Logo

Programming-Idioms

History of Idiom 26 > diff from v38 to v39

Edit summary for version 39 by Aquamo:
[Rust] make M, N constants, use x for array name

Version 38

2018-05-09, 01:38:57

Version 39

2018-05-09, 01:42:13

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() {
  let a: [[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!(a[0][0], 0.0);
  assert_eq!(a[0][3], 3.0);
  assert_eq!(a[3][0], 12.0);

  println!("the value at 2,2 should be 10.0, {}", a[2][2]);
}
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]);
}