Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

The File struct allows to create/read/overwrite files onto file systems. See Table 1 for some its methods. It implements the Read, Seek and io::Write traits that provide methods for moving cursor into file, read content and write content: see Table 2, Table 4 and Table 3.

Table 1:Some methods of File

Method

Description

open(p)

Opens a file in read mode.

create(p)

Opens a file in write mode. Creates a file if it does not exist or truncates it.

create_new(p)

Opens a file in write node. Returns an error if the file already exists.

options()

Equivalent to OpenOptions::new().

Table 2:Some methods of the Read trait

Method

Description

chain(o)

Chains another stream reader o after this reader. The other stream will start as soon as EOF is reached on this reader.

read(b)

Reads some bytes from this reader into the fixed size buffer b. Returns the number of bytes read.

read_exact(b)

Reads exactly the number of bytes required to fill the fixed size buffer b. If there are not enough bytes available, it returns an error.

read_to_end(b)

Reads all bytes from the stream up to EOF, placing into the dynamic size buffer b.

read_to_string(b)

Reads all bytes until EOF, and put them inside the String buffer b.

Table 3:Some methods of the Seek trait

Method

Description

rewind()

Rewinds to the start of the stream.

seek(p)

Moves the cursor to the position p in the stream. The given position must be a SeekFrom object, which can be expressed from the start of the stream, the current position, or the end. Returns the new position relative to the start of the stream.

seek_relative(offset)

Moves relative to the current position.

stream_position()

Returns the current position in the stream.

Table 4:Some methods of the Write trait

Method

Description

flush()

Flushes the buffer, and makes sure all data is written onto the destination.

write(b)

Writes the bytes buffer b into this writter. Returns the number of bytes written.

write_all(b)

Attempts to write the whole buffer b into this writer.

36.4.1Write mode

To create a new file, we may use the create() method:

A File object contains the following fields:

36.4.2OpenOptions

The OpenOptions object allows us to customize the opening of a file for reading or writing in any way possible. It is safer to use than the create() method.

To create an OpenOptions object, we may use OpenOptions::new() or File::options(). See Table 5 for a list of methods of OpenOptions. In the following example, we create a new OpenOptions object for which we enable writing, file creation, and truncation of any existing file at this path:

Table 5:Methods of OptionOptions

Method

Description

new()

Creates a new OpenOptions object with all options set to false.

append(b)

Enables or disables append mode.

create(b)

Enables or disables create mode. This mode creates a new file or opens it if it already exists.

create_new(b)

Enables or disables create new mode. This mode creates a new file or returns an error if it already exists.

open(p)

Opens a file at path p.

read(b)

Enables or disables read mode.

truncate(b)

Enables or disables truncate mode.

write(b)

Enables or disables write mode.

36.4.3The write!() and writeln!() macros

The write!() macro is used to write into a write stream using either an io::Write or a fmt::Write trait: it needs a write_fmt() method. The trait must be imported with the use keyword in order to be accessible for the write!() macro. It takes a as first argument the write stream (the file object in our case), and then a string or a format string, and eventually some arguments for the format string:

36.4.4Closing a file

Closing a file is done by letting the File object go out of scope. The closing provokes the flushing of the buffer and the release of the file descriptor. Here we implement a function that writes a file, the file will be closed when we leave the function:

36.4.5Appending to a file

To append to an existing file we just set the append flag:

36.4.6Read mode

The Read trait provides functions to access the content of a stream. To use it, we need to import with the use keyword. In the following example, we use the read_to_string() method to load the file’s content into a String instance:

36.4.7BufRead trait

The BufRead trait provides additional reading features like line by line reading (see Table 6). It is implemented for instance by the BufReader structure, which can be instantiated from any object implementing the Read trait: it will thus work on a File object.

Table 6:Methods of the BufRead trait

Method

Description

lines()

Returns an iterator over the lines of this reader.

read_line(s)

Reads all bytes until a Line Feed (LF or 0xA) character or EOF is reached, and appends to the string s.

read_until(b, v)

Reads all bytes until the byte b or EOF is reached, appends the bytes to the bytes vector v.

skip_until(b)

Skips all bytes up to byte b or EOF.

split(b)

Returns an iterator over chunks (i.e.: bytes vectors) from this reader, split on the byte b.

36.4.8Checking the error type

The various functions of the std::fs module and the methods of all its structures and traits return an io::Error object. Since file system errors are of many types, the io::Error structure offers a way to check what happened and possibly take action through the kind() method that returns an io::ErrorKind enum:

In this example: