我如何将其保存到文件而不是标准输出?

How would I save this to a file instead of stdout?

我可能只是漏掉了一些简单的东西 我用过 std::fs::File

use std::io::{stdout, Write};
use curl::easy::Easy;

fn main() {
    let mut easy = Easy::new();
    easy.url("checkip.amazonaws.com").unwrap();
    easy.write_function(|data| {
        Ok(stdout().write(data).unwrap())
    }).unwrap();
    easy.perform().unwrap();
}

将文件移动到闭包中并写入所有内容:

use std::fs::File;
use std::io::{Write};
use curl::easy::Easy;


fn main() {
    let mut file = File::create("foo.txt").expect("open file");
    let mut easy = Easy::new();
    easy.url("checkip.amazonaws.com").unwrap();
    easy.write_function(move |data| {
        file.write_all(data).expect("write data");
        Ok(data.len())
    }).unwrap();
    easy.perform().unwrap();
}