try_more

Crates.iotry_more
lib.rstry_more
version0.1.1
sourcesrc
created_at2022-02-01 19:05:03.695371
updated_at2022-02-01 19:16:11.217487
descriptionExpand your possibilities with the Try `?` Operator
homepage
repositoryhttps://github.com/dbofmmbt/try_more
max_upload_size
id525347
size7,842
Eduardo Canellas (dbofmmbt)

documentation

README

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.

Commit count: 4

cargo fmt