How do I write a formatted string to a file?

后端 未结 1 1785
鱼传尺愫
鱼传尺愫 2020-12-10 19:43

I want to write output of my function to a file. I expected that write_fmt is what I require:

use std::{
    fs::File,
    io::{BufWriter, Write},
};

fn mai         


        
1条回答
  •  难免孤独
    2020-12-10 20:13

    The documentation indicates the issue: the write_fmt method takes one argument, of type std::fmt::Arguments, which can be constructed via the format_args! macro. It also suggests the best way to use it:

    The write! macro should be favored to invoke this method instead.

    One calls write! (or writeln!) just like println!, but by passing the location to write to as the first argument:

    write!(&mut writer, "Factorial of {} = {}", num, factorial);
    

    (Note that the docs have a search bar at the top of each page, so one can find documentation on, for example, macros by searching for ! there.)

    0 讨论(0)
提交回复
热议问题