以下代码在所有通道中引发错误。 Rust语言服务器不会检测到此错误,并说类型是正确的。我出了什么问题?
use std::fs::File;
use std::io::Read;
use std::io::Write;
use std::env;
use std::string::String;
use std::str;
fn main() {
if let Some(file_name) = env::args().nth(2) {
let mut file_handle = File::open(file_name)
.expect(format!("Your File is invaild {}", &file_name as &str));
let mut text = String::new();
file_handle
.read_to_string(&mut text)
.expect("unable to read file");
text.replace("test", &env::args().nth(2).unwrap() as &str);
// file_handle.set_len(text.len() as u64).expect("could not resize file");
file_handle
.write(&text.as_bytes())
.expect("could not write to file");
} else {
println!("you did not give me a file")
}
}
error[E0308]: mismatched types
--> src/main.rs:11:21
|
11 | .expect(format!("Your File is invaild {}", &file_name as &str));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected &str, found struct `std::string::String`
|
= note: expected type `&str`
found type `std::string::String`