Skip to content

A tiny macro that implements a ternary operator for Rust

License

Notifications You must be signed in to change notification settings

spacekookie/ternop.rs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Ternary Operator

Rust doesn't support return (condition) ? if_true : if_false;. This crate exports a macro that implements this feature.

fn is_ipv4(val: &str) -> i32 {
    ternary!(val == "ipv4", 4, 16)
}

If you just want to copy the small macro, here you go 😅

#[macro_export]
macro_rules! ternary {
    ($condition: expr, $_true: expr, $_false: expr) => {
        if $condition { $_true } else { $_false }
    };
}

About

A tiny macro that implements a ternary operator for Rust

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages