是否有任何方法返回对在函数中创建的变量的引用?
OpenOptions.new().write()
trycreate()
trycreate()
use std::io::Write;use std::fs::OpenOptions;use std::path::Path;fn trycreate() -> &OpenOptions { let f = OpenOptions::new().write(true).open("foo.txt"); let mut f = match f { Ok(file) => file, Err(_) => panic!("ERR"), }; f}fn main() { { let f = OpenOptions::new().write(true).open(b"foo.txt"); let mut f = match f { Ok(file) => file, Err(_) => trycreate("foo.txt"), }; let buf = b"test1\n"; let _ret = f.write(buf).unwrap(); } println!("50%"); { let f = OpenOptions::new().append(true).open("foo.txt"); let mut f = match f { Ok(file) => file, Err(_) => panic!("append"), }; let buf = b"test2\n"; let _ret = f.write(buf).unwrap(); } println!("Ok");}
隔江千里