Home 2. mysterious @
Post
Cancel

2. mysterious @

Use @ to bind a pattern to a name.

1
2
3
4
5
6
let foo = 4;

match foo {
    x @ 0..=5 => assert_eq!(x,4),
    y @ _ => panic!("{}-too many!", y)
}
1
2
3
4
5
6
let point: (u32,u32) = (1, 2);

match point {
    (1, y @ _) => assert_eq!(y,2),
    _ => {}
}
1
2
3
4
5
let point: (u32,u32) = (1, 2);
let mypoint @ (x, _) = point;

assert_eq!(x,1);
assert_eq(mypoint,point);
This post is licensed under CC BY 4.0 by the author.