Rust Program | How To Read From A File in Rust | Grow Your Skill

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone welcome back I am Nish in this session we will see how to read some data from a file in the previous session we worked on creating a file writing some data into a new text file and uh I am going to reuse the same project instead of creating a new new one reason being that the way I am planning is that maybe uh we will refactor it accordingly in coming days and uh we will ask some user input something similar like press W for for writing some data press a for modify a file or press R for reading a file okay so this is my idea and uh what I'll do is that uh I'll refactor this code a little so I'll first create a new function and move all the code for writing a file into that function so FN write to file and uh bring all this code for writing into this function okay and uh also since this file name would be uh used across multiple places so what I'll do is I'll make it I'll move this outside as a global variable and we'll make it as a constant and we don't need to use the let keyword and S and also so for a constant so a convention is that the name should be all in a upper kease and we will uh replace the the our file name variable accordingly okay so that way the code for writing the the data into a file is successfully moved into write to file function and whenever we need to call it so very simply we will call this function and program would work uh as expected I'll comment it out and we will focus it on the reading app reading is also very very simple there is nothing too complex it is merely about three lines of code as a first step first we need to open the file I'll create a variable this would be a handle to our file f file and open function and we will need to provide the path so this is the this is file name and expect unable to open the file okay and by the way of course yes so it should be it must be un mutable otherwise we will not be able to perform the rest of the operations and next I will create a variable that will hold the the content of this file let mute file content I'll create a empty string and next we will read the file and we will place all the data from the file into the into this variable file content I'll use the file handle read to string function and we will pass on the variable file content as a mutable reference and unwrap POS it uh it returns a result and now we will very simply print the file content variable print Ln file content okay now let's try to test it cargo run hit enter so we are getting the file content maybe I'll we will have uh some new line for the clarity cargo run so we are getting it correctly and this is the same content what is uh present in the test.txt file so that's all for now I'll see you next so till then take care and happy coding
Info
Channel: Grow Your Skill
Views: 56
Rating: undefined out of 5
Keywords: Grow Your Skill, Rust, Rust introduction, cargo, rust lang, rust lang 2024, rust language, rust programming language, rust programming tutorial, rust tutorial, rustlang, read from file, file io in Rust, file reading and writing, file processing, file handling in rust, rust programming
Id: o2FZHotU5io
Channel Id: undefined
Length: 3min 17sec (197 seconds)
Published: Fri Feb 23 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.