How to create tuple with different types in Rust

3 Answers

0 votes
fn main() {
    let tuple = (100u8, 200u16, 300u32, 400u64,
                -100i8, -200i16, -300i32, -400i64,
                3.14f32, 19.873f64,
                'z', true);

    println!("tuple = {:?}", tuple);
    
    println!("tuple.0 = {}", tuple.0);
    println!("tuple.1 = {}", tuple.1);
    println!("tuple.2 = {}", tuple.2);
}





/*
run:

tuple = (100, 200, 300, 400, -100, -200, -300, -400, 3.14, 19.873, 'z', true)
tuple.0 = 100
tuple.1 = 200
tuple.2 = 300

*/

 



answered May 3, 2023 by avibootz
0 votes
fn main() {
    let tuple = (17, "rust", 3.14, -9);

    println!("tuple = {:?}", tuple);
    
    println!("tuple.0 = {}", tuple.0);
    println!("tuple.1 = {}", tuple.1);
    println!("tuple.2 = {}", tuple.2);
    println!("tuple.3 = {}", tuple.3);
}





/*
run:

tuple = (17, "rust", 3.14, -9)
tuple.0 = 17
tuple.1 = rust
tuple.2 = 3.14
tuple.3 = -9

*/

 



answered May 3, 2023 by avibootz
edited May 3, 2023 by avibootz
0 votes
fn main() {
    let tuple: (&str, u8, f32) = ("rust", 234, 3.14);

    println!("tuple = {:?}", tuple);
    
    println!("tuple.0 = {}", tuple.0);
    println!("tuple.1 = {}", tuple.1);
    println!("tuple.2 = {}", tuple.2);
}





/*
run:

tuple = ("rust", 234, 3.14)
tuple.0 = rust
tuple.1 = 234
tuple.2 = 3.14

*/

 



answered May 3, 2023 by avibootz
edited May 3, 2023 by avibootz

Related questions

2 answers 194 views
2 answers 139 views
139 views asked May 3, 2023 by avibootz
1 answer 102 views
102 views asked May 3, 2023 by avibootz
1 answer 119 views
1 answer 131 views
1 answer 121 views
...