From the course: Rust Essential Training

Unlock the full course today

Join today to access over 22,500 courses taught by industry experts or purchase this course individually.

Writing to files

Writing to files - Rust Tutorial

From the course: Rust Essential Training

Start my 1-month free trial

Writing to files

- [Instructor] After your program has generated some useful data, you may want to to write it to a file to use it again later. For example, the program shown here builds a string containing part of president Kennedy's famous speech about going to the moon. Those historic words are worth saving, so let's write them to a file. The fs module contains a simple function for writing to file, so we'll start by importing that. Then, in the main function, we'll call the write function from the fs module. This function takes two input arguments: the first argument is a path to the file we want to write. Let's save this as speech.text. And then the second argument is the contents you want to write to the file. For that, we'll pass in the speech string. And that's all there is to it. When I run this program, it creates a new file named speech.text in the project directory and when I opened that up, we can see that it contains those…

Contents