#002 Jan 2022match pettern2. 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); show more
#001 Jan 2022macro match1. matches!Need to check if an expression matches certain pattern?1 2 let foo = vec!['1','2']; assert!(matches!(foo.len(), 0..=1)); The example above checks if len is 0 or 1.Or1 2 3 let bar = Some(4); assert!(matches!(bar, Some(x) if x > 2)); show more