Skip to content

add a macro rule open_file! #12912

Closed
Closed
@electricface

Description

@electricface
macro_rules! open_file(
    ($path_str:expr) => (
        ::std::io::File::open(&Path::new( $path_str ))
    );
    ($path_str:expr, rw ) => ( 
        ::std::io::File::open_mode(&Path::new( $path_str ) , ::std::io::Open,
        ::std::io::ReadWrite )
    ); 
    ($path_str:expr, w ) => ( 
        ::std::io::File::open_mode(&Path::new( $path_str ) , ::std::io::Open,
        ::std::io::Write )
    );
    ($path_str:expr, a ) => ( 
        ::std::io::File::open_mode(&Path::new( $path_str ) , ::std::io::Append,
        ::std::io::Write )
    );  
)

example

fn main(){
    let file_path="message.txt";
    let mut file = try_or!(
        open_file!(file_path ,w ),
        "can't open file `{}` : [{}]", file_path,e
        );
}

about macro rule try_or! see #12911

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions