Rust Collection Types
Array
A collection of items of the same type.
Arrays are of fixed sizes, the items can be changed later as it's mut
by default but no additions to the items or removing from it.
To create an array
let arr = [1,3,4,5];
...one might also create an array of repeated item like this:
fn main(){
let arr = ["abc"; 32];
println!("{:?}", arr) // this will print ["abc","abc",....,"abc"] 32 "abc"
}
This is useful when creating a buffer fro later use in a system.
let buffer = [0u8; 560]
One can perform a slice operation on an array by using the range like feature:
fn main(){
let arr = [1,2,3,5,4,5];
let a_portion = &arr[1..4]
}
A &
is needed because the compiler doesn't know the size and a slice can be of any size.
[1..4]
will print from the first index up until third index omitting the 4th index this is called exclusive range
If we want it to be inclusive range then we have to add =
before the ending index, [1..=4]
will include item on the fourth index.
Vector
So think of Vector
and Array
like String
and &str
respectively, if String
has more information and capacity than &str
so does Vector
to Array
You can create a vector just as the same way you create a string but with:
new
keyword
let mut my_vec: Vec<i8> = Vec::new();
my_vec.push(2)
my_vec.push(98)
vec![]
macro
let mut my_vec = vec![3,56,45]
Anything can be put inside a vector:
Vec<String>
: A vector of strings
Vec<Vec<String>>
: A vector that contains a vector of strings
Vec<(i8, i8)>
: A vector of a tuple of i8
You can also perform a slice operation on a vector
fn main(){
let my_vec: Vec<i8> = vec![2,5,6,8,23];
let first_slice = &my_vec[0..=2];
println!("{:?}", first_slice);
}
Make an Array
into a Vec
The into()
keyword also works on arrays as well, arrays can be converted into vectors.
fn main(){
let my_vec : Vec<i8> = [1,5,60].into();
}
The compiler can also infer the type of a vector intelligently by giving it an underscore(_
); Vec<_>
.
fn main(){
let my_vec : Vec<_> = [1,5,60].into(); // this will make a Vec<i32>
}
Tuples
Tuples are important part in rust as a function by default if nothing is returned will return an empty tuple and this is referred to as the unit type.
so fn some_func() -> (){}
is the same as fn some_func2(){}
Tuples can hold together different types of data.
let my_tuple = (1,[2,3,4], "a string");
The are access with indexes but also with the dot notation(.
)
so instead of saying my-tuple[1]
we will have my_tuple.1
Multiple variables can be created from a tuple as well.
let my_strings = ("one".to_string(), "two".to_string(), "three".to_string(), );
let (a, b, c) = my_strings;
println!("{a}"); // will print "one"
what happen at let (a,b,c)
is called destructuring and it works only if the patterns on both side match as Rust can't tell what you are trying to do when they don't match.
let (a,b) = my_strings
Rust can't tell if "b" should be "two" or "three"
But if we wanted to really ignore a value we can insert and (_
), then Rust can tel we don't care about that value.
let (_,b,c) = my_strings
Rust can tell we don't want the value "one"