首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >锈蚀:相同的代码编译时有警告或错误失败。

锈蚀:相同的代码编译时有警告或错误失败。
EN

Stack Overflow用户
提问于 2022-01-09 11:52:07
回答 1查看 215关注 0票数 1

我试图在Windows上跟踪本教程

代码语言:javascript
复制
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)

我在两个项目目录中有以下代码:

代码语言:javascript
复制
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),
        }
    }
}

删除targetCargo.lock之后,在一个项目中,我得到以下输出:

代码语言:javascript
复制
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

而在另一边我得到了

代码语言:javascript
复制
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

知道为什么它在一个项目中编译得很好,而在另一个项目中却失败了吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-09 14:02:19

panic!宏的行为与锈蚀2021版本发生了一些变化,以使其与其他格式的家族宏更加一致。迁移指南的整整一章致力于此。

修复程序和包含详细信息的链接也显示在您获得的错误消息中:

代码语言:javascript
复制
  = 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);
  |     ~~~~~~~~~~~~~~~~~~~~~
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70640990

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档