Weird Expressions in Rust
10 months ago
- #Type System
- #Rust
- #Unconventional Patterns
- Rust's type system allows for some unusual expressions, demonstrated in the `weird-expr.rs` test file.
- The `never` type (`!`) can coerce into any other type, enabling expressions like `return true` assigned to a boolean.
- Examples include using `return` in place of `()` in function parameters and returning `while` loops.
- Rust's weak keywords, like `union`, allow for creative naming in certain contexts.
- Range syntax (`..`, `..=`) can be combined in various ways, all implementing `Debug`.
- Assignment expressions can be chained with empty tuples (`()`), showcasing Rust's flexibility.
- Unicode characters can be used for identifiers, bypassing keyword restrictions.
- The `Deref` trait enables recursive function calls through smart pointer-like behavior.
- Match guards can include nested `if` statements, and patterns can be chained with `|`.
- The `matches!` macro and wildcard patterns allow for complex, albeit unconventional, logic.