Syntactic sugar that tastes weird.
:=
Python 3.8+
Allows values to be assigned as part of an expression.
if (n := len(my_list)) > 10:
print(f"List is too long ({n} elements)")
()|_|
Rust
A closure that takes no parameters and returns nothing.
Similar but not completely interchangeable with
std::mem::drop
.
let my_value = String::from("hello");
// my_value is moved into the closure and then dropped
let _ = (|_| ())(my_value);
::<>
Rust
The base syntax for explicitly specifying type parameters for generic functions or methods. 1
let x = "42".parse::<i32>().unwrap();
Result<(), ()>
Rust
A result type with ()
as both the success and error types.
Often used for operations that succeed or fail without returning any meaningful value.
Interestingly, Result<(), ()>
generates one fewer assembly instruction than bool
and Option<()>
.
fn perform_action(success: bool) -> Result<(), ()> {
if success {
Ok(())
} else {
Err(())
}
}
<=>
Ruby, Perl, PHP, C++20
Provides three-way comparison between two values.
5 <=> 10 # => -1
10 <=> 10 # => 0
15 <=> 10 # => 1
?:
Kotlin, PHP, C, C++
Used to provide a default value when a value is falsy.
A ?: B
is approximately equivalent to the ternary operator A ? A : B
.
Resembles Elvis Presley's hairstyle.
val name = userInput ?: "Guest"
Reverse the typical order of a conditional statement, with the constant on the left side.
To prevent accidental assignment (=
) instead of comparison (==
) in languages where both operators are allowed in conditional statements.
if (42 == value) {
// Do something
}
See turbo.fish, the Bastion of the Turbofish and its explanation. ↩