Created
July 1, 2024 22:40
-
-
Save romyilano/60a0edb4c7349848393b551bc23e553f to your computer and use it in GitHub Desktop.
handling a vector and array. Note that the &a is a reference to the elements of array a which are on the stack. Vectors live on the heap.
This file contains 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 tuple = array_and_vec(); | |
println!("{:?} array and {:?} vector", tuple.0, tuple.1); | |
} | |
fn array_and_vec() -> ([u32; 4], Vec<u32>) { | |
let a = [1, 25, 200, 203]; | |
let mut v = Vec::new(); | |
// reference symbol in a (a pointer to the stack) | |
for element in &a { | |
// * is the derefernce with * | |
v.push(*element); | |
} | |
(a, v) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment