Crates.io | turnip |
lib.rs | turnip |
version | 1.1.3 |
source | src |
created_at | 2024-01-12 23:44:09.150854 |
updated_at | 2024-01-16 15:56:13.720826 |
description | Ternary operators for Rust |
homepage | https://github.com/admercs/turnip-rs |
repository | https://github.com/admercs/turnip-rs |
max_upload_size | |
id | 1098154 |
size | 38,613 |
Ternary operators (turnips) for Rust.
Rather than creating a functional procedural macro to parse yet-another domain-specific syntax (a language within a language), turnip
provides the simplest possible solution. The result is recursion without additional function calls and consistency with Rust syntax rules, which does not support overloading the ?
and :
operators.
The solution is a single 10-line macro defined using macro_rules!
. See for yourself. Unlike other solutions out there, such as terny, tern, iffy and ternop, turnip
combines simplicity with built-in support for recursion to more closely match a ternary operator design pattern.
What more do you need?
cargo add turnip
Create a new crate and add turnip
:
cargo init
cargo add turnip
Open src/main.rs
and import the ifelse!
macro:
// main.rs
use turnip::ifelse;
fn main() {
let result1: bool = ifelse!(10 < 0, true, false);
let result2: bool = ifelse!(10 < 0, true, 10 == 0, true, false);
assert!(result1 == result2);
}
Compile and run the project:
cargo build
cargo run
MIT License