Answers for "read file contents in rust"

4

read file contents in rust

use std::fs;

fn main() {
    let data = fs::read_to_string("file_name.txt").expect("Unable to read file");
    println!("{}", data);
}
Posted by: Guest on September-13-2021
-3

read file rust

use std::env;
use std::fs;

fn main() {
    // --snip--
    let args: Vec<String> = env::args().collect();

    let query = &args[1];
    let filename = &args[2];

    println!("Searching for {}", query);
    println!("In file {}", filename);

    let contents = fs::read_to_string(filename)
        .expect("Something went wrong reading the file");

    println!("With text:\n{}", contents);
}
Posted by: Guest on October-12-2020

Browse Popular Code Answers by Language