3 ms·
If you were to open a file that didn't exist and then not use the result at all, then you get a warning (which can be made an error, if you want): File::op
by andolanra 7y ago
If you were to open a file that didn't exist and then not use the result at all, then you get a warning (which can be made an error, if you want):
File::open("foo");
// warning: unused `Result` that must be used
But that's also not necessarily an invalid program, it's just not a very useful one. Failing to open a file doesn't interrupt the program in any way, so File::open here will return either a file handle or an error, neither of which get used. However, if you open a file, don't check for an error, but still try to use the file, then your program won't compile:
File::open("foo").read(&mut buffer);
// error: method `read` not found in `Result<File, Error>`
You need to be explicit about what to do in case the file didn't open, and Rust won't let you use your file handle unless you explicitly indicate what to do in that error case!