代码:
//#![windows_subsystem = "windows"] use std::{io::Read,io::Write}; use std::fs::{File, OpenOptions}; static _NETDISK:&str = "\\192.168.3.2\共享文件\"; static LOCALDISK:&str = "D:\临时文件夹\"; fn main() { let buf = b"not allow zh-CN"; write_file(LOCALDISK.to_owned() + "a.txt",buf).unwrap(); println!("{:?}",read_file(LOCALDISK.to_owned() + "a.txt")); } fn read_file(fname:String) -> Option<String>{ let mut contents = String::new(); let mut file= match std::fs::File::open(fname ){ Ok(f) => f, Err(_) => return None, }; match file.read_to_string(&mut contents){ Ok(_) => return Some(contents), Err(_) => return None, }; } pub fn write_file(fname:String,buf:&[u8]) -> Result<(),std::io::Error> { let fo = OpenOptions::new() .create(true) // 新建,若文件存在则打开这个文件 .write(true) //.append(true) // 追加内容 .open(fname); let mut f:File = match fo { Ok(f)=>f, Err(e)=>return Err(e), }; match f.write_all(buf){ Ok(_)=>Ok(()), Err(e)=>return Err(e), } }