Create a two-dimensional array at runtime: Difference between revisions

Content added Content deleted
m (→‎{{header|Rust}}: Added comment explanations)
(→‎{{header|Rust}}: Fixed argument handling)
Line 1,937:
<lang rust>use std::env;
fn main() {
let mut args = env::args().skip(1).flat_map(|num| num.parse());
let rows = args.nthnext(1).unwrap().parse().unwrapexpect("Expected number of rows as first argument");
let cols = args.nthnext(2).unwrap().parse().unwrapexpect("Expected number of columns as second argument");
 
assert!(rows != 0 && cols != 0);
 
// Creates a vector of vectors with all elements initialized to 0.
let mut v = init_vec(rows, || init_vec(cols, || 0, cols), rows);
v[rows-10][cols-10] = 41;
println!("{}", v[rows-10][cols-10]);
 
 
Line 1,954:
// initialized with the values computed by `f`
 
fn init_vec<F,T>(fn: Fusize, nf: usizeF) -> Vec<T>
where F: Fn() -> T
{
let mut vec = Vec::with_capacity(n);
for i_ in 0..n {
vec.push(f());
}