primitives.rs (1207B)
1 use std::fmt; 2 3 struct Matrix(f32, f32, f32, f32); 4 5 fn transpose(m: Matrix) -> Matrix { 6 // `let` can be used to bind the members of a tuple to variables 7 Matrix(m.0, m.2, m.1, m.3) 8 } 9 10 impl fmt::Display for Matrix { 11 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 12 write!(f, "( {} {} )\n( {} {} )", self.0, self.1, self.2, self.3) 13 } 14 } 15 16 fn main() { 17 let matrix = Matrix(1.1, 1.2, 2.1, 2.2); 18 println!("Matrix:\n{}", matrix); 19 println!("Transpose::\n{}", transpose(matrix)); 20 21 //i = 5; 22 println!("ARRAYS AND SLICES"); 23 // Arrays: Fixed length, known at compile time 24 let arr_five_nums: [i32; 5] = [1, 5, 3, 4, 2]; 25 let arr_ten_zeros: [i32; 10] = [0; 10]; 26 println!("{:?}", arr_five_nums); 27 println!("{:?}", arr_ten_zeros); 28 // These line will break the build or, if `let i = 5` is moved up, 29 // lead to panic 30 //// let i = 5; 31 //// println!("{}", arr_five_nums[i]); 32 // 33 // Slices: Similar to arrays, but length unknown at compile time. 34 // Essentially consists of (pointer to first element, length) 35 // Arrays can be automatically borrowed as slices 36 let slice: &[i32] = &arr_five_nums[1..4]; 37 println!("{:?}", slice); 38 }