我正在学习Rust,我有点难过.
我正在尝试为用户提供将输出写入stdout或提供的文件名的选项.
我开始与那正是使用给出的示例代码extra::getopts位于这里.从那里,在do_work函数中,我正在尝试这样做:
use std::io::stdio::stdout;
use std::io::buffered::BufferedWriter;
fn do_work( input: &str, out: Option<~str> ) {
println!( "Input: {}", input );
println!( "Output: {}", match out {
Some(x) => x,
None => ~"Using stdout"
} );
let out_writer = BufferedWriter::new( match out {
// I know that unwrap is frowned upon,
// but for now I don't want to deal with the Option.
Some(x) => File::create( &Path::new( x ) ).unwrap(),
None => stdout()
} ); …Run Code Online (Sandbox Code Playgroud)