
[ad_1]
This article will review how you can create, open a file, read its contents, and write to it in Rust.
As a software engineer or someone interested in technologies, read and write (I/O for input and output) operations are some essential concepts you should know.
After reading this article, you will be able to create, open a file, read its contents and add content to it. So let’s dive.
But first, let’s lay some important groundwork by reviewing how the characters are represented in Rust.
I know this title (☝🏿) sounds a bit confusing… In Rust, we have two types to represent strings:
str
:string slice, the only string type in the core Rust programming language. This type is stable and immutable, and we usually see it in its borrowed form:&str
,String
: is the second string type made available in Rust, thanks to the standard library. This type is more flexible, as it can be owned and is mutable.
both are string types UTF-8 encoded (more on that below) Here, String
This is used when you need to own or change your string and &str
When you want to visualize a string.
The Rust standard library allows us to perform a variety of file operations for terrifying structures. File
and related methods. We’re going to take advantage of that in the rest of this article.
We are all set now, so let’s jump right into it!!
How do you create a file?
To create a file, we are going to use create
a method of structuring File
As shown in the example below:
create
method returns a Result
As we may succeed in the creation of the file, or we may have an error (disk full, no privileges, etc., choose your poison).
Result
One enum Rust is also provided by the standard library, which allows us to manage errors (more Here,
How do you open a file?
To open the file, we’re going to use open
method of structure File
which a. also returns Result
,
Here open
Will try to locate the file at the root level of our Rust project. If the file does not exist, it will be created.
I encourage you to run our previous example with an existing and non-existent file to see the results.
How do you read the contents of a file?
The simple way to read the contents of a file is to open it and store its contents in a . read as String
(Now you see why this was the first section to introduce string types).
As you can see, we create a variable String
named variable contents
and we are updating it by passing the content of the file on line 7 using the method read_to_string()
, I encourage you to check the method read
which allows you to retrieve the contents of a file as a bytes vector (more on this below) Here,
I like to read the contents of the file using struct BufReader<R>
Because it is more efficient and optimal. Cheat is that Bufreader
Provides a buffering component when reading from a file (more on that below) Here,
As you can see, we started our example by opening a file, as we learned earlier. Then on line 6, we’re making reader
variable; One BufReader
On the file we just opened. after that we will use reader
To extract the contents of the file (line 8).
How do you add some content to a file?
Adding content is not an easy task, as we want to add some content to the new file, overdrive the content of the existing file or add some content to an existing file.
What seemed trivial a few seconds ago now appears to be more complicated… Welcome (back) to the beauty of software development.
So let’s start with the first two cases; The following example shows you how to add content to a newly created file or open an existing file and override its contents.
In our example, if the file missy.txt
does not exist, we will create it before adding our content. If the file exists, our new content will replace its contents.
yes we are using create
once again. create
Allow us to create files and open existing ones in write-only mode.
using the create
Works on existing files, but if we want to be more precise, we should use structOpenOptions
to determine how the file is accessed.
In this example, we open an existing file and add some content to the beginning. If the file contains some content, our new content will be added at the beginning, replacing only the amount of data needed (be careful here; we set append
options to false
on line 9).
In the last example below, we’ll add some content to an existing data file by appending it to the end. see here append
options to true
on line 9.
Like the writing process, Rust has a BufWriter
structwhich provides an optimal way to write to a stream.
I hope I can give you a solid foundation for doing some file I/O operations in Rust. this is just the begining. I encourage you to explore the documentation of the structure File
and then dive deep BufReader
One BufWriter
,

[ad_2]
Source link
#File #Reading #Writing #Rust #Moose #Anees #August