The following code works and I don't know why:
File::open(&some_path).read_to_end().unwrap();
Looking at the API docs I can see File::open()
returning a IoResult
which does not have a read_to_end()
.
Is there some kind of syntax sugar going on? Does Result<T, Error>
somehow turn into Result<U, Error>
?
Documentation: http://doc.rust-lang.org/std/io/fs/struct.File.html#method.read_to_end
Open a file for read only. File pointer starts at the beginning of the file Open a file for write only. Erases the contents of the file or creates a new file if it doesn't exist. File pointer starts at the beginning of the file Open a file for write only. The existing data in file is preserved. File pointer starts at the end of the file.
After the body has been completed, the file is automatically closed, so it can't be read without opening it again. But wait! We have a line that tries to read it again, right here below: Let's see what happens:
A subsequent call to open this file with CreateFile will succeed if the call uses the same access and sharing modes. Tip: You can use the file you created with the previous WriteFile example to test this example.
A better method to open files is with the fopen () function. This function gives you more options than the readfile () function. The first parameter of fopen () contains the name of the file to be opened and the second parameter specifies in which mode the file should be opened.
read_to_end
is from the Reader
trait and if you look there you can see that there is an implementation for reader for IoResult<R>
for any R
that implements Reader
:
impl<R: Reader> Reader for IoResult<R>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With