顯示和除錯

YouTube 上觀看本章內容

Rust 中單純的變數可以在 println! 裡用 {}1 來被印出。但是有些變數不能,你需要用 除錯列印(debug print)。除錯列印是給程式設計師用的列印方法,因為它通常會顯示更多的資訊。除錯(Debug)有時看起來並不漂亮,因為它有額外的資訊來幫助你。

你怎麼知道你是否需要 {:?}2 而不是 {}?編譯器會告訴你。比如說:

fn main() {
    let doesnt_print = ();
    println!("This will not print: {}", doesnt_print); // ⚠️
}

當我們執行這個程式時,編譯器會說:

error[E0277]: `()` doesn't implement `std::fmt::Display`
 --> src\main.rs:3:41
  |
3 |     println!("This will not print: {}", doesnt_print);
  |                                         ^^^^^^^^^^^^ `()` cannot be formatted with the default formatter
  |
  = help: the trait `std::fmt::Display` is not implemented for `()`
  = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
  = note: required by `std::fmt::Display::fmt`
  = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

這有相當多的資訊,但重要的部分是 you may be able to use {:?} (or {:#?} for pretty-print) instead。這意味著你可以試試 {:?},也可以試試 {:#?}{:#?} 叫做"漂亮列印"。它和 {:?} 一樣,但是用更多行和不同的格式印出內容。所以 Display 意思是用 {} 列印,Debug 則是用 {:?} 列印。還有一點:如果你不想要換行,你也可以使用 print! 而不需要有 ln

fn main() {
    print!("This will not print a new line");
    println!(" so this will be on the same line");
}

這個將會印出 This will not print a new line so this will be on the same line

1

譯註: 即顯示列印 (Display print)。

2

譯註: 除錯列印的格式。

最小和最大的數

如果你想知道最小和最大的數字,你可以在型別名稱後使用 MIN 和 MAX:

fn main() {
    println!("The smallest i8 is {} and the biggest i8 is {}.", i8::MIN, i8::MAX); // 提示: 印出 std::i8::MIN 表示 "列印在標準函式庫裡 i8 型別的 MIN 值"
    println!("The smallest u8 is {} and the biggest u8 is {}.", u8::MIN, u8::MAX);
    println!("The smallest i16 is {} and the biggest i16 is {}.", i16::MIN, i16::MAX);
    println!("The smallest u16 is {} and the biggest u16 is {}.", u16::MIN, u16::MAX);
    println!("The smallest i32 is {} and the biggest i32 is {}.", i32::MIN, i32::MAX);
    println!("The smallest u32 is {} and the biggest u32 is {}.", u32::MIN, u32::MAX);
    println!("The smallest i64 is {} and the biggest i64 is {}.", i64::MIN, i64::MAX);
    println!("The smallest u64 is {} and the biggest u64 is {}.", u64::MIN, u64::MAX);
    println!("The smallest i128 is {} and the biggest i128 is {}.", i128::MIN, i128::MAX);
    println!("The smallest u128 is {} and the biggest u128 is {}.", u128::MIN, u128::MAX);

}

將會印出:

The smallest i8 is -128 and the biggest i8 is 127.
The smallest u8 is 0 and the biggest u8 is 255.
The smallest i16 is -32768 and the biggest i16 is 32767.
The smallest u16 is 0 and the biggest u16 is 65535.
The smallest i32 is -2147483648 and the biggest i32 is 2147483647.
The smallest u32 is 0 and the biggest u32 is 4294967295.
The smallest i64 is -9223372036854775808 and the biggest i64 is 9223372036854775807.
The smallest u64 is 0 and the biggest u64 is 18446744073709551615.
The smallest i128 is -170141183460469231731687303715884105728 and the biggest i128 is 170141183460469231731687303715884105727.
The smallest u128 is 0 and the biggest u128 is 340282366920938463463374607431768211455.