Macro iffy::i[][src]

i!() { /* proc-macro */ }

Ternary operator macro.

Modeled after the ternary operator in C-like languages.

Examples

Basic usage

// Include the macro
use iffy::i;

// Define some variables to use in the
// with the macro, so that we keep things
// neat
let a = 20;
let b = 30;

// Use the macro in place of Rust's `if..else`
// expressions, for more compact code
let result = i!(a > b ? "a wins" : "b wins");

// Make sure `b` is the winner
assert_eq!(result, "b wins");

Nested usage

The crate currently does not support nested ternary syntax, so a temporary variable is needed to simulate this.

// Include the macro
use iffy::i;

// Define some variables to use in the
// with the macro, so that we keep things
// neat
let a = 20;
let b = 30;
let c = 50;

// Temporary variables are required for
// nesting ternary operators
let tmp0 = i!(b > c ? "b wins" : "c wins");
let tmp1 = i!(a > c ? "a wins" : "c wins");

// Use the macro in place of Rust's `if..else`
// expressions, for more compact code
let result = i!(b > a ? tmp0 : tmp1);

// Make sure `c` is the winner
assert_eq!(result, "c wins");
assert_eq!(tmp0, "c wins");
assert_eq!(tmp1, "c wins");