• JSP
  • RubyonRails
  • Struts
  • JavaScript
  • SQL Tutorial
  • Core Java
  • Spring
  • Servlet
  • Java IO

  • <

    Rust File Handling

    Rust allow us perform read-write operations on a file.

    Rust File Methods


    Module Method Description
    std::fs::File open() The open static method can be used to open a file in read-only mode.
    std::fs::File create() Static method opens a file in write-only mode. If the file already existed, the old content is destroyed. Otherwise, a new file is created.
    std::fs::remove_file remove_file() Removes a file from the filesystem. There is no guarantee that the file is immediately deleted.
    std::fs::OpenOptions append() Sets the option for the append mode of file.
    std::io::Writes write_all() Attempts to write an entire buffer into this write.
    std::io::Read read_to_string() Reads all bytes until EOF in this source, appending them to buf.

    Rust Write a File

    We use Create() method to create a file d.txt

    
    use std::io::Write;
    fn main() {
       let mut f = std::fs::File::create("d.txt").expect("create failed");
       file.write_all("Hello World".as_bytes()).expect("write failed");
       println!("data written successfully" );
    }
    

    Output:
    data written successfully
    

    Rust Read a File


    use std::io::Read;
    
    fn main(){
       let mut file = std::fs::File::open("d.txt").unwrap();
       let mut contents = String::new();
       file.read_to_string(&mut contents).unwrap();
       print!("{}", contents);
    }
    

    Output:
    Hello World
    
















    © copyright 2017-2021 Completedone pvt ltd.