Rust- arrays Flashcards

1
Q

Features of array

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Decalaring arrays

A

//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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How to use iter() function

A

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);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Pass by value for array

A

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);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Pass by value for reference

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly