Created
August 27, 2016 20:49
-
-
Save greyblake/47fe9522f6d7edb3928e15a61cf8ef72 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
let v1 = vec![4, 8, 12, 16]; | |
let v2 = mult(&v1, 10); | |
pv("v1", &v1); | |
pv("v2", &v2); | |
println!(""); | |
println!("{}", v1[2]); | |
} | |
fn mult(v : &Vec<i32>, x : i32) -> Vec<i32> { | |
let mut result : Vec<i32> = Vec::with_capacity(v.len()); | |
for val in v { | |
result.push(*val * x); | |
} | |
result | |
} | |
fn pv(name : &str, v : &Vec<i32>) { | |
let last_i = v.len() - 1; | |
print!("{} = ", name); | |
print!("["); | |
for (i, val) in v.iter().enumerate() { | |
if i == last_i { | |
print!("{}", val); | |
} else { | |
print!("{}, ", val); | |
} | |
} | |
print!("]\n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment