|
ToDo:
|
写経してみました。
use std::error::Error;
use std::fs::File;
use std::io::prelude::*;
use std::path::Path;
fn main(){
let path = Path::new(".\\hello.txt");
let display = path.display();
let mut file = match File::open(&path) {
Err(why) => panic!("cound't open{}: {}",display,
Error::description(&why)),
Ok(file) => file,
};
let mut s = String::new();
match file.read_to_string(&mut s) {
Err(why) => panic!("cound't read {}: {}",display,
Error::description(&why)),
Ok(_) => print!("{} contains:\n{}",display,s),
}
}
読み込みファイルがないとき
thread 'main' panicked at 'cound't open .\hello.txt: entity not found', open.rs:11:20
note: Run with `RUST_BACKTRACE=1` for a backtrace.
exit status 101
ファイルがあるけどエンコードがutf-8でないとき、このときはiso-2022-jpでした。
thread 'main' panicked at 'cound't read .\hello.txt: stream did not contain valid UTF-8', open.rs:17:20 note: Run with
`RUST_BACKTRACE=1` for a backtrace. exit status 101
でRUST_BACKTRACEを1 にしろってことなので
set RUST_BACKTRACE=1
にして実行したところ
thread 'main' panicked at 'cound't read .\hello.txt: stream did not contain valid UTF-8', open.rs:17:20
stack backtrace:
0: std::sys_common::backtrace::_print
at C:\projects\rust\src\libstd\sys_common\backtrace.rs:94
1: std::panicking::default_hook::{{closure}}
at C:\projects\rust\src\libstd\panicking.rs:380
2: std::panicking::default_hook
at C:\projects\rust\src\libstd\panicking.rs:397
3: std::panicking::rust_panic_with_hook
at C:\projects\rust\src\libstd\panicking.rs:611
4: std::panicking::begin_panic
at C:\projects\rust\src\libstd\panicking.rs:572
5: std::panicking::begin_panic_fmt
at C:\projects\rust\src\libstd\panicking.rs:522
6: __STATIC_FMTARGS
7: panic_unwind::__rust_maybe_catch_panic
at C:\projects\rust\src\libpanic_unwind\lib.rs:99
8: std::rt::lang_start
at C:\projects\rust\src\libstd\rt.rs:54
9: main
10: __scrt_common_main_seh
at f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl:259
11: BaseThreadInitThunk
exit status 101
と出てきたのはよいが…どーすりゃーええんぢゃ?