What is the difference between these two cases? Why does the commented line compile but the last line in the main is incorrect?
How to cut string (substr
in C++ with non-const arguments) without an extra variable?
use std::fs::File;
use std::io;
use std::io::Read;
fn read_string(filename: &str) -> Result<String, io::Error> {
let mut s = String::new();
File::open(filename)?.read_to_string(&mut s)?;
Ok(s)
}
fn main() {
let s = read_string("tt.txt").expect("Wow");
// let s2: String = s.chars().skip(0).take(s.len() -2).collect();
println!(
"{}",
s.chars().skip(0).take(s.len() - 2).collect() as String
);
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…