Skip to content

Instantly share code, notes, and snippets.

@romyilano
Created July 1, 2024 22:40
Show Gist options
  • Save romyilano/60a0edb4c7349848393b551bc23e553f to your computer and use it in GitHub Desktop.
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.
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