代码如下:
use std::fs;
fn main() {
let input=fs::read_to_string(".\\input.txt").unwrap();
println!("{}",input);
fs::write(".\\output.txt",input).unwrap();
}
input.txt和output.txt如下:
Hello,World!I'm Microsoft!
运行结果如下:
PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Rust\rust-learning-path\hello-world> rustc main.rs
PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Rust\rust-learning-path\hello-world> .\main.exe
Hello,World!I'm Microsoft!
PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Rust\rust-learning-path\hello-world>
如果是cargo的话,不能把input.txt和output.txt放在scr,而是放在cargo文件夹下,输入cargo run的那个根目录下。
本文介绍了如何使用Rust语言通过`fs`模块进行文件的读取和写入,以`input.txt`和`output.txt`为例,展示了基本的I/O操作。同时提到在Cargo项目中文件位置的要求。
4047

被折叠的 条评论
为什么被折叠?



