Skip to content

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

@m104ngc4594

Description

@m104ngc4594

页码与行数

  • 代码清单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"));
   |         ^^^

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions