Rust- arrays Flashcards
Features of array
An array declaration allocates sequential memory blocks.
Arrays are static. This means that an array once initialized cannot be resized.
Each memory block represents an array element.
Array elements are identified by a unique integer called the subscript/ index of the element.
Populating the array elements is known as array initialization.
Array element values can be updated or modified but cannot be deleted.
Decalaring arrays
//Syntax1
let variable_name = [value1,value2,value3];
//Syntax2
let variable_name:[dataType;size] = [value1,value2,value3];
//Syntax3
let variable_name:[dataType;size] = [default_value_for_elements,size];
How to use iter() function
let arr: [i32; 4] = [10,20,30,40];
println!(“array is {:?}”,arr);
println!(“array size is :{}”,arr.len());
for val in arr.iter(){
println!(“value is :{}”, val);
}
Pass by value for array
fn main() {
let arr = [10,20,30];
update(arr);
print!(“Inside main {:?}”,arr);
}
fn update(mut arr:[i32;3]){
for i in 0..3 {
arr[i] = 0;
}
println!(“Inside update {:?}”,arr);
}
Pass by value for reference
fn main() {
let mut arr = [10,20,30];
update(&mut arr);
print!(“Inside main {:?}”,arr);
}
fn update(arr:&mut [i32;3]){
for i in 0..3 {
arr[i] = 0;
}
println!(“In