第16章 格式化输出

格式化输出 — Rust By Practice

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

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

格式化输出

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
fn main() {
    // `{}` 会被参数自动替换并转成字符串
    println!("{} days", 31);

    // 默认 31 是 i32,可用后缀改类型,比如 31i64

    // 可以使用位置参数
    println!("{0}, this is {1}. {1}, this is {0}", "Alice", "Bob");

    // 也可以使用具名参数
    println!("{subject} {verb} {object}",
             object="the lazy dog",
             subject="the quick brown fox",
             verb="jumps over");

    // `:` 后可指定特殊格式
    println!("{} of {:b} people know binary, the other half doesn't", 1, 2);

    // 右对齐并指定宽度,输出 "     1"(5 个空格 + "1")
    println!("{number:>width$}", number=1, width=6);

    // 用 0 填充,输出 "000001"
    println!("{number:0>width$}", number=1, width=6);

    // Rust 会检查参数个数是否匹配
    println!("My name is {0}, {1} {0}", "Bond");
    // FIXME ^ 补上缺失的参数 "James"

    // 定义一个包含 i32 的结构体
    #[allow(dead_code)]
    struct Structure(i32);

    // 自定义类型需要实现格式化,否则不会工作
    println!("This struct `{}` won't print...", Structure(3));
    // FIXME ^ 注释掉这一行

    // 1.58+ 支持直接捕获外部变量
    // 同样输出 "     1"(5 个空格 + "1")
    let number: f64 = 1.0;
    let width: usize = 6;
    println!("{number:>width$}");
}

[std::fmt][fmt] 提供了一组控制文本展示的 [traits][traits],两类常用基石:

  • fmt::Debug:使用 {:?},偏调试输出
  • fmt::Display:使用 {},偏用户友好输出

这里用 fmt::Display 是因为标准库已为这些类型实现它;自定义类型要打印则需要额外步骤。

为类型实现 fmt::Display 也会自动实现 [ToString],可用于把类型 [转换][convert] 成 [String][string]。


01-println! 和 format!

println! 和 format! — Rust By Practice

02-Debug 和 Display

Debug 和 Display — Rust By Practice

03-格式化

格式化 — Rust By Practice

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