gw_rust_programming_tutorial/chapter_12/testfile.rs
1use std::fs;
2use std::error::Error;
3
4pub fn test_file()
5{
6 if let Err(e) = test_file_read()
7 {
8 println!("err={}",e);
9 }
10}
11
12fn test_file_read()->Result<(),Box<dyn Error>>
13{
14 //使用?表示 如果函数发生错误返回error给调用者去处理
15 let content = fs::read_to_string("c:\\2.txt")?;
16
17 println!( "file content = {}" ,content);
18 Ok(())
19}