Files
rust-by-example/src/primitives.rs
2024-08-13 08:54:05 +02:00

125 lines
3.7 KiB
Rust

#![allow(dead_code)]
fn reverse_tuple(pair: (i32, bool)) -> (bool, i32) {
let (int_param, bool_param) = pair;
(bool_param, int_param)
}
pub fn primitives_module() {
// Scalar types
// signed integers: i8, i16, i132, 164, i128
// signed integers: u8, u16, u132, u64, u128
// floating point: f32, f64
// char
// bool
// unit type (), only posible value is an empty tuple
////
// Compound types
// Array [1,2,3]
// Tuples (1, true)
let logical = true; // not necesary to specify the type if initialized
let a_float: f64 = 1.0;
let rare_integer = 5i32; // suffix annotation, = integer of 32 bits with a value of 5
println!("{} {} {}", logical, a_float, rare_integer);
// All are inmutable by default
// mut keyword
let mut mutable_int = 12i32;
println!("int: {}", mutable_int);
mutable_int = 21;
println!("int mutated: {}", mutable_int);
let mutable_int = true; // variables can be overwritten with shadowing
println!("not an int anymore {}", mutable_int);
let thousand = 1_000;
println!(
"1_000 is a good way to express {} in order to make it readable",
thousand
);
let cientific = 7.6e-4;
println!(
"Cientific notation is also valid, as 7.6e-4 would be stored as {}",
cientific
);
println!(
"Remember that the type can be specified with the number, as 1u32 is {}",
1u32
);
let my_tuple = (1, "corcho", true);
println!(
"The second value of the tuple {:?} is {}",
my_tuple, my_tuple.1
);
let another_tuple = (1i32, false);
println!(
"A function can also receive and return tuples, such as reverse return {:?} on {:?}",
reverse_tuple(another_tuple),
another_tuple
);
#[derive(Debug)]
struct EmulatedMatrix(f32, f32, f32, f32);
let matrix = EmulatedMatrix(1.1, 1.4, 0.5, 7.3);
println!("Simulated: {:?}", matrix);
let EmulatedMatrix(a, b, c, d) = matrix;
println!("Data can also be destructured: {}, {}, {}, {}", a, b, c, d);
// EXERCISE 1: fmt::Display Matrix (make it easy, you dont know loops yet)
println!("EXERCISE 1: Print a matrix with the tuple struct:");
use std::fmt::{Display, Formatter, Result};
impl Display for EmulatedMatrix {
fn fmt(&self, f: &mut Formatter) -> Result {
write!(f, "({}, {})\n({} {})", &self.0, &self.1, &self.2, &self.3)
}
}
println!("{}", matrix);
// EXERCISE 2: transpose a EmulatedMatrix (make it easy, you dont know loops yet)
println!("EXERCISE 2: make a simple transpose function (later on we'll improve it with loops)");
fn transpose(m: EmulatedMatrix) -> EmulatedMatrix {
let EmulatedMatrix(a, b, c, d) = m;
return EmulatedMatrix(a, c, b, d);
}
println!("{}", transpose(matrix));
// Arrays
let mut onetofive: [i8; 5] = [1, 2, 3, 4, 5];
println!("The fourth element is {}", onetofive[3]);
let allthrees: [i32; 10] = [3; 10];
println!(
"All threes: {:?} have a size of {}",
allthrees,
allthrees.len()
);
use std::mem;
println!("All threes accupies {} bytes", mem::size_of_val(&allthrees));
// Sclice: pointer to a portion of an array
// '&' means borrowing
let onetothree = &mut onetofive[1..3];
println!("One to three slice {:?}", onetothree);
onetothree[1] = 7;
println!("onetofive slice {:?} after mutating onetothree", onetofive);
for i in 0..onetofive.len() + 1 {
// force a failure
match onetofive.get(i) {
Some(xval) => println!("{}: {}", i, xval),
None => println!("Slow down! {} is too far!", i),
}
}
}