01-println! 和 format!

println! 和 format! — Rust By Practice

内容来源 · Rust By Practice / Rust 语言实战

原文链接: https://practice-rust-zh.beatai.org/formatted-output/println.html

println! 和 format!

[std::fmt][fmt] 中有一组用于打印的宏,例如:

  • format!:写入到 [String][string]
  • print!:同 format!,但输出到 stdout
  • println!:print! 并追加换行
  • eprint!:输出到 stderr
  • eprintln!:eprint! 并追加换行

所有这些宏的格式解析方式一致,且编译期检查格式正确性。

format!

1.🌟

1
2
3
4
5
6
7

fn main() {
    let s1 = "hello";
    /* 填空 */
    let s = format!(__);
    assert_eq!(s, "hello, world!");
}

print!、println!

2.🌟

 1
 2
 3
 4
 5
 6
 7
 8
 9
10

fn main() {
   /* 填空,让输出变为:
   Hello world, I am 
   Sunface!
   */
   __("hello world, ");
   __("I am");
   __("Sunface!");
}

参考答案在这里(solutions 路径):https://github.com/sunface/rust-by-practice/blob/master/solutions/formatted-output/println.md,仅在需要时再看哦 :)

最后修改 August 21, 2026: 更新 (76fc81a2e)