Created
August 27, 2017 12:06
-
-
Save illuzian/3792c14f655138ead3301ec07b0ccf3c to your computer and use it in GitHub Desktop.
Rust code with comments describing references for modification of a value
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() { | |
// This function modifies the values passed in | |
// Takes in a Vec with types i32 as a reference & and mutable | |
fn addition(in_vec: &mut Vec<i32>) { | |
// We've already referenced in_vec so no need for & | |
for x in in_vec { | |
// We need to dereference using * | |
*x = *x + 10; | |
} | |
} | |
// Mutable vector to be passed in to our function | |
let mut test_vec: Vec<i32> = vec![1, 2, 3]; | |
// Call our function and pass in referenced mutable version of test_vec | |
addition(&mut test_vec); | |
// Print our results | |
for i in test_vec { | |
println!("Result {}", i); | |
} | |
} | |
//Outputs: | |
//Running `target/debug/learn` | |
//Result 11 | |
//Result 12 | |
//Result 13 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment