影片地址
頭條地址:https://www.ixigua.com/i676544267458235648...
B站地址:https://www.bilibili.com/video/av78062009?...
網易雲課堂地址:https://study.163.com/course/introduction....
github地址
讀取檔案
將檔案讀取成二進位制,使用read函式
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();
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結