Skip to content

add a macro rule try_or! #12911

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
electricface opened this issue Mar 15, 2014 · 2 comments
Closed

add a macro rule try_or! #12911

electricface opened this issue Mar 15, 2014 · 2 comments

Comments

@electricface
Copy link

macro_rules! try_or(
    ($e:expr, $action:expr ) => (
        match $e {
            Ok(o) => o,
            Err(e) => $action
        }
    );

    ($e:expr,$fmt:expr , $($arg:tt)* ) => (
        match $e {
            Ok(o) => o, 
            Err(e) =>  { fail!($fmt, $($arg)*)}
        }
    );

)
    let mut file = try_or!(
        File::open_mode(p, Open, ReadWrite ),
        "can't open file `{}` : [{}]",p.display(),e
        );

or

    let mut file = try_or!( 
        File::open_mode( &Path::new("message.txt"), Open, ReadWrite),
        {
            error!("can't open file");
            return
        }
    );
@lifthrasiir
Copy link
Contributor

As with your other issues, I think you need some rationales to suggest those macros. Try! got included in the standard library since the same pattern pops up here and there; you have to show the same thing for try_or!, possibly by pointing the recurring patterns in the standard library.

@alexcrichton
Copy link
Member

Closing for reasons found here. Additionally, the try! macro is designed to be as lightweight as possible. To customize its functionality, I recommend using a match statement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants