天天看點

Rust程式設計知識拾遺:Rust 程式設計,讀取檔案

視訊位址

頭條位址:https://www.ixigua.com/i6765442674582356483

B站位址:https://www.bilibili.com/video/av78062009?p=

github位址

​​github位址​​

讀取檔案

use std::fs;

fn main() {
    let context = fs::read("tt").unwrap();
    println!("context: {:#?}", context);
}      
use std::fs;

fn main() {
    let context = fs::read_to_string("tt").unwrap();
    println!("context: {}", context);
}      

讀取目錄

use std::io;
use std::fs;
use std::path::Path;

fn visit_dirs(dir: &Path) -> io::Result<()> {
    if dir.is_dir() {
        for entry in fs::read_dir(dir)? {
            let entry = entry?;
            let path = entry.path();
            if path.is_dir() {
                visit_dirs(&path)?;
            } else {
                let c = fs::read_to_string(path).unwrap();
                println!("file = {}", c);
            }
        }
    }
    Ok(())
}

fn main() {
    //let context = fs::read("tt").unwrap();
    //println!("context: {:#?}", context);

    //let context = fs::read_to_string("tt").unwrap();
    //println!("context: {}", context);

    visit_dirs(Path::new("./test")).unwrap();
}      

繼續閱讀