我想将函数的输出写入文件。我希望write_fmt
符合我的要求:
use std::{
fs::File,
io::{BufWriter, Write},
};
fn main() {
let write_file = File::create("/tmp/output").unwrap();
let mut writer = BufWriter::new(&write_file);
// From my function
let num = 1;
let factorial = 1;
writer.write_fmt("Factorial of {} = {}", num, factorial);
}
错误
error[E0061]: this function takes 1 parameter but 3 parameters were supplied
--> src/main.rs:11:12
|
11 | writer.write_fmt("Factorial of {} = {}", num, factorial);
| ^^^^^^^^^ expected 1 parameter
这似乎是错误的,文档中没有多少可用内容。
答案 0 :(得分:0)
use std::fs::File;
use std::io::Write;
fn main() {
let mut w = File::create("/tmp/test.txt").unwrap();
writeln!(&mut w, "test").unwrap();
writeln!(&mut w, "formatted {}", "arguments").unwrap();
}