Fun fact: TupleStruct { 0: value, 1: value }


#1

I just spotted this crazy syntax for initializing and pattern matching tuple structs and newtypes in the Rust reference.

#[derive(Eq, PartialEq, Debug)]
struct Wat(u32);

fn main() {
    assert_eq!(
        Wat(32),
        Wat { 0: 32 } // wat
    );
    
    match Wat(32) {
        Wat { 0: 32 } => {},
        _ => panic!(),
    }
}

#2

Considering that tuple struct field accesses works via numbers, i.e. assert_eq!(wat.0, 32), a corresponding initialization syntax doesn’t seem all that farfetched to me.


#3

#4

This can be used with struct update syntax as well.

#[derive(Debug)]
struct Type(i32, i32, i32, i32, i32, i32, i32, i32, i32, i32);

fn main() {
    let x = Type(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
    let y = Type { 1: 5, ..x };
    println!("{:?}", y);
}

This is useful when dealing with macros, as tuples don’t have to special cased.


#5
struct S(i32);

fn foo(s: S) {
    let x: i32 = s.0000000000000000000000000000000000000000000000000000000000000;
}

:duck::duck::duck:

This is a reported bug though, hopefully it’ll get fixed soon.