#ternary #condition #proc-macro #if

macro iffy

proc macro for simulating the ternary operator from C-like languages

3 releases

0.1.3 Feb 7, 2021
0.1.2 Feb 7, 2021
0.1.1 Feb 7, 2021

#1442 in Procedural macros

MIT license

7KB

iffy-rs

Crates.io Docs.rs

Rust proc macro for simulating the ternary operator from C-like languages.


This crate defines a macro to imitate the ternary operator found in C-like languages such as C, C++, Java, etc. The macro can be used to make more compact conditional expressions in Rust code.

For example, this code in plain rust:

let a = 20;
let b = 30;
 
// This is the part we will be able to simplify
let min = if a < b {
    a
} else {
    b
};
 
// Check the result
assert_eq!(min, a);

... Can be shortened to the following, with this crate:

let a = 20;
let b = 30;

// Shortened from the previous example
let min = iffy::i!(a < b ? a : b);

// Check the result
assert_eq!(min, a);

lib.rs:

This crate defines a macro to imitate the ternary operator found in C-like languages such as C, C++, Java, etc. The macro can be used to make more compact conditional expressions in Rust code.

For example, this code in plain rust:

let a = 20;
let b = 30;

// This is the part we will be able to simplify
let min = if a < b {
    a
} else {
    b
};

// Check the result
assert_eq!(min, a);

... Can be shortened to the following, with this crate:

let a = 20;
let b = 30;

// Shortened from the previous example
let min = iffy::i!(a < b ? a : b);

// Check the result
assert_eq!(min, a);

Dependencies

~1.5MB
~37K SLoC