- Representam um único valor contido dentro de uma escala conhecida.
- Permitem a comparação direta entre valores.
- inteiro (integer) ex:
5
- flutuante (floating point) ex:
42.1
- booleano (bool) ex:
true
,false
- caractere (char) ex:
'a'
,'ॐ'
,'🙂️'
- Servem para agregar multiplos valores.
- Tupla (tuple) ex:
(5, true, 42.1, 'a')
- Matriz (array) ex:
[1, 2, 3, 4, 5, 6]
bits | signed | unsigned |
---|---|---|
8 | i8 | u8 |
16 | i16 | u16 |
32 | i32 | u32 |
64 | i64 | u64 |
128 | i128 | u128 |
arch | isize | usize |
range: -(2n- 1) até 2n- 1 - 1
i8: -128 até 127 [-(
range: 0 até
fn main() {
let x = 5;
let y: i32 = 5;
let z = 5_i32;
}
fn main() {
let x: i8 = 5;
let y: u8 = x - 20; //error
}
fn main() {
let x: i8 = 5;
let y: i8 = x * 200; //error
}
fn main() {
let x = 5;
let y = 199_456_898_9;
let h = 0xff;
let o = 0o77;
let b = 0b1111_0000;
let by = b'A';
}
fn main() {
let x = 42.1;
let x: f64 = 42.1; // f64 padrão -> precisão dupla
let x: f32 = 42.1;
}
fn main() {
let x = true;
let x: bool = false;
}
- Armazena até 4 bits da tabela unicode.
fn main() {
let letra = 'a';
let letra: char = 'a';
let letra = '😁';
}
fn main() {
//tupla
let numbers = (1, 2, 3);
println!("{:?}", numbers);
let numbers: (i32, i32, i32) = (1, 2, 3);
println!("{:?}", numbers);
let numbers: (i32, i32, f64) = (1, 2, 3.5);
println!("{:?}", numbers);
}
fn main() {
//tupla
let numbers = (1, 2, 3);
println!("{:?}", numbers.1);
}
fn main() {
//tupla
let numbers = (1, 2, 3);
println!("{:?}", numbers);
let (a, b, c) = numbers;
println!("{:?}", a);
}
fn main() {
//tupla
let mut numbers = (1, 2, 3);
println!("{:?}", numbers);
numbers.0 = 50;
println!("{:?}", numbers);
}
fn main() {
//tupla
let mut numbers = (1, 2, 3);
println!("{:?}", numbers);
numbers = (4, 5, 6); //pattern match
println!("{:?}", numbers);
}
fn main() {
let numbers = [1, 2, 3];
println!("{:?}", numbers);
let numbers: [i32;3] = [1, 2, 3];
println!("{:?}", numbers);
}
fn main() {
let numbers = [1.1, 2.0, 3.3];
println!("{:?}", numbers[0]);
}
fn main() {
let mut numbers = [1.1, 2.0, 3.3];
println!("{:?}", numbers[0]);
numbers[0] = 50.3;
println!("{:?}", numbers[0]);
}
fn main() {
let mut numbers = [1.1, 2.0, 3.3];
println!("{:?}", numbers);
numbers = [4.4, 5.5, 6.6];
println!("{:?}", numbers);
}
fn main() {
let mut numbers = [1.1, 2.0, 3.3];
println!("{:?}", numbers[10]); //error
}
fn main() {
let mut numbers = [1.1, 2.0, 3.3];
println!("{:?}", numbers);
//slice
println!("{:?}", &numbers[1..3]);
println!("{:?}", &numbers[1..]);
println!("{:?}", &numbers[..2]);
}