#language #macro #utility #no-std #rust

no-std ternary-rs

A Rust library for emulating the ternary operator from C/C++ (among other languages)

1 stable release

1.0.0 Jan 15, 2021

#2961 in Rust patterns

Download history 6/week @ 2024-07-20 21/week @ 2024-07-27 36/week @ 2024-08-03 18/week @ 2024-08-10 29/week @ 2024-08-17 41/week @ 2024-08-24 35/week @ 2024-08-31 61/week @ 2024-09-07 46/week @ 2024-09-14 60/week @ 2024-09-21 52/week @ 2024-09-28 39/week @ 2024-10-05 34/week @ 2024-10-12 29/week @ 2024-10-19 49/week @ 2024-10-26 36/week @ 2024-11-02

150 downloads per month

MIT/Apache

6KB

ternary-rs

A Rust library for emulating the ternary operator from C/C++ (among other languages)

Exposes two macros that do the same thing:

  • Choose between two macros ifelse! and ternary
ifelse!(condition, true value, false value)
ternary!(condition, true value, false value)

Usage

Add this to your Cargo.toml:

[dependencies]
ternary-rs = "1.0.0"

Example

let cond = true;
let result = if_else!(cond, 1, 0);
assert_eq!(1, result);
assert_ne!(0, result);

// If you find if_else!() unclear, you can use the ternary!() macro and be explicit instead!
let result = ternary!(cond, 1, 0);
assert_eq!(1, result);
assert_ne!(0, result);

License

ternary-rs is distributed under the terms of both the MIT license and the Apache License (Version 2.0).

See LICENSE-APACHE and LICENSE-MIT for details.

No runtime deps