try_more 0.1.1

Expand your possibilities with the Try `?` Operator
Documentation
  • Coverage
  • 100%
    8 out of 8 items documented1 out of 8 items with examples
  • Size
  • Source code size: 7.52 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.21 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 10s Average build duration of successful builds.
  • all releases: 10s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • dbofmmbt/try_more
    1 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • dbofmmbt

Expand your possibilities with the Try ? Operator

crate documentation

Have you ever found yourself writing a function which may return early based on some condition?

fn my_function() {
    // ...

    if condition_a {
        return;
    }
    
    // ...

    if condition_b {
        return;        
    }
    
    // ...
}

It doesn't look Rusty, right? This crate offers an extension trait to be able to convert from a bool to a ControlFlow and leverage the mighty power of ? to get rid of those checks:

use core::ops::ControlFlow;
use try_more::BoolFlow;

fn my_function() -> ControlFlow<()> {
    // ...

    BoolFlow::r#break(condition_a)?;

    // ...

    condition_b.r#break()?;
    
    // ...
  
}

There's also other methods besides continue and break which allows to control the value which is passed to the Break variant.