Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can I call File::open(...).read_to_end()?

Tags:

rust

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

like image 501
Kai Sellgren Avatar asked Sep 19 '14 18:09

Kai Sellgren


People also ask

How to open a file for read only and write only?

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.

Why can't I read a file after it has been completed?

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:

Can a subsequent call to CreateFile open a file?

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.

What is the difference between ReadFile () and fopen ()?

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.


1 Answers

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>
like image 55
Arjan Avatar answered Sep 28 '22 21:09

Arjan