我正在尝试在 Windows 上遵循本教程
D:\src\rust\hecto>cargo --version
cargo 1.57.0 (b2e52d7ca 2021-10-21)
D:\src\rust\hecto>rustc --version
rustc 1.57.0 (f1edd0429 2021-11-29)
Run Code Online (Sandbox Code Playgroud)
我在 2 个项目目录中有以下代码:
use std::io;
use std::io::Read;
fn die(e: std::io::Error) {
    panic!(e);
}
fn main() {
    let ctrl_q = 'q' as u8 & 0b1_1111;
    for b in io::stdin().bytes() {
        match b {
            Ok(b) => {
                let c = b as char;
                if c.is_control() {
                    println!("{:#b} \r", b);
                }
                else {
                    println!("{:?} ({})\r", b, c);
                }
                if b == ctrl_q {
                    break;
                }
            },
            Err(err) => die(err),
        }
    }
}
Run Code Online (Sandbox Code Playgroud)
删除target和后Cargo.lock,在一个项目中我得到以下输出:
C:\temp\hecto-tutorial-die-on-input-error>cargo build
    Updating crates.io index
   Compiling winapi v0.3.9
   Compiling cfg-if v1.0.0
   Compiling parking_lot_core v0.8.5
   Compiling smallvec v1.7.0
   Compiling scopeguard v1.1.0
   Compiling bitflags v1.3.2
   Compiling instant v0.1.12
   Compiling lock_api v0.4.5
   Compiling crossterm_winapi v0.9.0
   Compiling parking_lot v0.11.2
   Compiling crossterm v0.22.1
   Compiling hecto v0.1.0 (C:\temp\hecto-tutorial-die-on-input-error)
warning: panic message is not a string literal
 --> src\main.rs:5:12
  |
5 |     panic!(e);
  |            ^
  |
  = note: `#[warn(non_fmt_panics)]` on by default
  = note: this usage of panic!() is deprecated; it will be a hard error in Rust 2021
  = note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/panic-macro-consistency.html>
help: add a "{}" format string to Display the message
  |
5 |     panic!("{}", e);
  |            +++++
help: or use std::panic::panic_any instead
  |
5 |     std::panic::panic_any(e);
  |     ~~~~~~~~~~~~~~~~~~~~~
warning: `hecto` (bin "hecto") generated 1 warning
    Finished dev [unoptimized + debuginfo] target(s) in 13.22s
Run Code Online (Sandbox Code Playgroud)
而在另一个我得到
D:\src\rust\hecto>cargo build
    Updating crates.io index
   Compiling winapi v0.3.9
   Compiling parking_lot_core v0.8.5
   Compiling cfg-if v1.0.0
   Compiling scopeguard v1.1.0
   Compiling smallvec v1.7.0
   Compiling bitflags v1.3.2
   Compiling instant v0.1.12
   Compiling lock_api v0.4.5
   Compiling crossterm_winapi v0.9.0
   Compiling parking_lot v0.11.2
   Compiling crossterm v0.22.1
   Compiling hecto v0.1.0 (D:\src\rust\hecto)
error: format argument must be a string literal
 --> src\main.rs:5:12
  |
5 |     panic!(e);
  |            ^
  |
help: you might be missing a string literal to format with
  |
5 |     panic!("{}", e);
  |            +++++
error: could not compile `hecto` due to previous error
Run Code Online (Sandbox Code Playgroud)
知道为什么它在一个项目中编译良好但在另一个项目中失败吗?
Rust 2021 版本中宏的行为panic!发生了一些变化,以使其与其他格式系列宏更加一致。迁移指南的一整章都专门讨论这一点。
您收到的错误消息中还指出了修复程序以及包含详细信息的链接:
  = note: `#[warn(non_fmt_panics)]` on by default
  = note: this usage of panic!() is deprecated; it will be a hard error in Rust 2021
  = note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/panic-macro-consistency.html>
help: add a "{}" format string to Display the message
  |
5 |     panic!("{}", e);
  |            +++++
help: or use std::panic::panic_any instead
  |
5 |     std::panic::panic_any(e);
  |     ~~~~~~~~~~~~~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)
        |   归档时间:  |  
           
  |  
        
|   查看次数:  |  
           470 次  |  
        
|   最近记录:  |