Scalar types integer, float, bool, char
Compound types tuple and array.
#![allow(unused)]
fn main() {
let tup: (i32, f64, u8) = (500, 6.4, 1);
let five_hundred = x.0;
}
Values accessed by zero based indexing.
#![allow(unused)]
fn main() {
let a: [i32; 5] = [1,2,3,4,5];
let a: [i32; 5] = [0; 5];
let last = a[4];
}
Fixed size. Stack allocated. Zero based indexing without the .
.
Can be mutated.
#![allow(unused)]
fn main() {
let mut a = [1,2,3,4,5];
a[4] = 0;
}
Can be sliced (range end is exclusive or inclusive (=i
)).
#![allow(unused)]
fn main() {
let mut a = [1,2,3,4,5];
let b = &a[0..=4];
}
Resizable generic collection.
#![allow(unused)]
fn main() {
let mut my_vec: Vec<String> = Vec::new();
my_vec.push(name1);
my_vec.push(name2);
}
Can also declare with a macro:
#![allow(unused)]
fn main() {
let mut some_numbers = vec![4,5,6];
}
Can slice:
#![allow(unused)]
fn main() {
&some_numbers[0..]
}
Or declare with a size:
#![allow(unused)]
fn main() {
let mut letters: Vec<char> = Vec::with_capacity(8);
println!("{}", letters.capacity());
}
#![allow(unused)]
fn main() {
enum ThingsInTheSky {
Sun,
Stars,
}
ThingsInTheSky::Sun
}
Values can have values, thus they are discriminated unions.
#![allow(unused)]
fn main() {
enum Feature {
Note(u8),
Rest(duration: u8),
}
let sound = Feature::Note(7);
let no_sound = Feature::Rest(250);
}
and they can have static functions and methods:
#![allow(unused)]
fn main() {
impl Feature {
fun play(&self) {}
fun new_rest(duration: u8) -> Self {
Feature::Rest {
duration
}
}
}
}