ZhangHanDong / tao-of-rust-codes

《Rust编程之道》随书源码
https://ruststudy.github.io/tao_of_rust_docs/tao_of_rust/
MIT License
1.18k stars 170 forks source link

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

Open m104ngc4594 opened 1 year ago

m104ngc4594 commented 1 year ago

页码与行数


文本或排版错误

暂无


代码错误

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 commented 1 year ago

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