Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[第7章]7.1.1结构体的代码清单7-9中input没有必要是mut,而且try应该用问号替代 #339

Open
m104ngc4594 opened this issue Feb 21, 2023 · 1 comment

Comments

@m104ngc4594
Copy link

页码与行数

  • 代码清单7-9
  • 第4-7行

文本或排版错误

暂无


代码错误

impl fmt::Display for ColoredString {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut input = &self.input.clone(); // 此处应该是let input = &self.input.clone();
        try!(f.write_str(&self.compute_style())); // 此处应该是f.write_str(&self.compute_style())?;
        try!(f.write_str(input)); // 此处应该是f.write_str(input)?;
        try!(f.write_str("\x1B[0m")); // 此处应该是f.write_str("\x1B[0m")?;
        Ok(())
    }
}

Rust版本

$ rustc -V
rustc 1.67.1 (d5a82bbd2 2023-02-07)

错误信息

warning: variable does not need to be mutable
  --> src/color.rs:78:13
   |
78 |         let mut input = &self.input.clone();
   |             ----^^^^^
   |             |
   |             help: remove this `mut`
   |
   = note: `#[warn(unused_mut)]` on by default
warning: use of deprecated macro `try`: use the `?` operator instead
  --> src/color.rs:79:9
   |
79 |         try!(f.write_str(&self.compute_style()));
   |         ^^^
   |
   = note: `#[warn(deprecated)]` on by default

warning: use of deprecated macro `try`: use the `?` operator instead
  --> src/color.rs:80:9
   |
80 |         try!(f.write_str(input));
   |         ^^^

warning: use of deprecated macro `try`: use the `?` operator instead
  --> src/color.rs:81:9
   |
81 |         try!(f.write_str("\x1B[0m"));
   |         ^^^
@ZhangHanDong
Copy link
Owner

感谢反馈。这个代码过期了。try! 已经过期,被 ? 替代。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants