Henry Lee

Interesting Syntax

August 7, 2025

Syntactic sugar that tastes weird.


#Walrus Operator :=

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)")

#Toilet Closure ()|_|

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);

#Turbofish ::<>

Rust


The base syntax for explicitly specifying type parameters for generic functions or methods. 1

let x = "42".parse::<i32>().unwrap();

#Owl 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(())
    }
}

#Spaceship Operator <=>

Ruby, Perl, PHP, C++20


Provides three-way comparison between two values.

5 <=> 10   # => -1
10 <=> 10  # => 0
15 <=> 10  # => 1

#Elvis Operator ?:

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"

#Yoda Conditions

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
}

#Footnotes

  1. See turbo.fish, the Bastion of the Turbofish and its explanation.


henrlly [at] icloud [dot] com
github.com/henrlly
linkedin.com/in/henrlly
RSS