Is it possible to declare local anonymous structs in Rust?

While anonymous structs aren’t supported, you can scope them locally, to do almost exactly as you’ve described in the C version:

fn main() {

    struct Example<'a> {
        name: &'a str
    };

    let obj = Example { name: "Simon" };
    let obj2 = Example { name: "ideasman42" };

    println!("{}", obj.name); // Simon
    println!("{}", obj2.name); // ideasman42
}

Playground link

One other option is a tuple:

fn main() {

    let obj = (1, 0, 1);

    println!("{}", obj.0);
    println!("{}", obj.1);
    println!("{}", obj.2);
}

Playground link

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)