Create a two-dimensional array at runtime: Difference between revisions
Content added Content deleted
(→{{header|Rust}}: Added Rust) |
m (→{{header|Rust}}: Added comment explanations) |
||
Line 1,943: | Line 1,943: | ||
assert!(rows != 0 && cols != 0); |
assert!(rows != 0 && cols != 0); |
||
// Creates a vector of vectors with all elements initialized to 0. |
|||
let mut v = |
let mut v = init_vec(|| init_vec(|| 0, cols), rows); |
||
v[rows-1][cols-1] = 4; |
v[rows-1][cols-1] = 4; |
||
println!("{}", v[rows-1][cols-1]); |
println!("{}", v[rows-1][cols-1]); |
||
Line 1,950: | Line 1,951: | ||
} |
} |
||
// Returns a dynamically-allocated array of size `n`, |
|||
⚫ | |||
// initialized with the values computed by `f` |
|||
⚫ | |||
⚫ | |||
⚫ | |||
{ |
{ |
||
let mut vec = Vec::with_capacity( |
let mut vec = Vec::with_capacity(n); |
||
for i in 0.. |
for i in 0..n { |
||
vec.push(f( |
vec.push(f()); |
||
} |
} |
||
vec |
vec |