ralfbiedert / cheats.rs

Rust Language Cheat Sheet - https://cheats.rs
https://cheats.rs
4.15k stars 394 forks source link

Document that `@` pattern can now introduce new bindings #148

Closed maxwase closed 3 years ago

maxwase commented 3 years ago

Since Rust 1.56.0 you can run this code:

fn main() {
    let a @ b @ c = 1;
    println!("{} {} {}", a, b, c); // 1 1 1
}

PR playground

ralfbiedert commented 3 years ago

Jesus that's hideous ...

ralfbiedert commented 3 years ago

How is that even supposed to work, if I do

let mut a @ mut b @ c = 1;
b = 2;
dbg!(a);

It prints 1, but aren't they meant to bind the same memory location?

...

I mean, ok, in line with the above this here doesn't compile:

struct S(u8);

let a @ b @ c = S(123);

So in a sense this doesn't really bind the underlying thing trice, but just creates 3 copies. This is probably the ugliest language concept I've come across but I like your example for outlining this weird behavior. I'd love if someone could sleuth a good (authoritative) link we can add for further reading.