Why does the Rust Language Server not warn about expected &str, found struct std::string::String?

419 Views Asked by At

The following code raises an error in all channels. The Rust Language Server does not detect this error and says the types are correct. What did I get wrong?

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`
0

There are 0 best solutions below