I wrote a program to generate such a invalid string
fn main() {
let s = "fn main(){
let s = \"\rabc\";
}";
std::fs::write("./out.rs", s).unwrap();
}
If we use rustc out.rs, then the compiler will emit the error, however, if we use cargo run to run the resulting code, there is no error. What's the reason?
That's my oversight. Paste the code from out.rs to main.rs in vscode will change \r to \n, and saving the code in vscode editor also does. If directly copy out.rs into src directory and rename its name to main.rs, the compiler will emit the error
error: bare CR not allowed in string, use \r instead
Note that it is not possible that rustc emits an error while cargo compiles happily. Cargo is not a separate compiler! It's merely an orchestration program that calls the compiler on packages/files. If invoking rustc directly fails due to a syntax or semantic error, then invoking it through cargo will fail as well.
I agree in principle. At least for syntax errors; possible errors when using rustc manually (and incorrectly) are errors regarding missing dependencies that cargo would resolve by passing the appropriate command line arguments to rustc. And actually… even for syntax errors… say… the code fails with rustc because its syntax isn’t compatible with edition 2015 (the default chosen when rustc is invoked without the --edition=… argument), whilst the Cargo.toml specifies a later edition; that’s a possibility.