box-o-sand/rustbyexample/primitives/primitives_tuples.rs

46 lines
1.1 KiB
Rust
Raw Normal View History

2021-09-11 17:26:27 +00:00
use std::fmt;
2021-09-10 18:24:08 +00:00
fn reverse(pair: (i32, bool)) -> (bool, i32) {
let (integer, boolean) = pair;
(boolean, integer)
}
#[derive(Debug)]
struct Matrix(f32, f32, f32, f32);
2021-09-11 17:26:27 +00:00
impl fmt::Display for Matrix {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "( {} {} )\n( {} {} )", self.0, self.1, self.2, self.3)
}
}
2021-09-10 18:24:08 +00:00
fn main() {
let long_tuple = (
1u8, 2u16, 3u32, 4u64, -1i8, -2i16, -3i32, -4i64, 0.1f32, 0.2f64, 'a', true,
);
println!("long tuple first value: {}", long_tuple.0);
println!("long tuple second value: {}", long_tuple.1);
let tuple_of_tuples = ((1u8, 2u16, 2u32), (4u64, -1i8), -2i16);
println!("tuple of tuples: {:?}", tuple_of_tuples);
let pair = (1, true);
println!("pair is {:?}", pair);
println!("the reversed pair is {:?}", reverse(pair));
println!("one element tuple: {:?}", (5u32,));
println!("just an integer: {:?}", (5u32));
let tuple = (1, "hello", 4.5, true);
let (a, b, c, d) = tuple;
println!("{:?}, {:?}, {:?}, {:?}", a, b, c, d);
let matrix = Matrix(1.1, 1.2, 2.1, 2.2);
println!("{:?}", matrix);
2021-09-11 17:26:27 +00:00
println!("{}", matrix);
2021-09-10 18:24:08 +00:00
}