< 返回版块

newbie 发表于 2020-06-23 13:15

exercise/generics/generics3.rs 这个题我想的是把结构体里的grade字段弄成T ,但是不知道怎么给T实现 fmt::Display 有没有大哥给个答案或者例子


Ext Link: https://github.com/rust-lang/rustlings/blob/main/exercises/generics/generics3.rs

评论区

写评论
作者 newbie 2020-06-23 17:47

哇哦 你早就做完啦

--
👇
gwy15: https://github.com/gwy15/rustlings/commit/c4554b48fccdc612d935cafbe71124a153904d83#diff-5ece68a5c6fc6606e542676d78b6c051

作者 newbie 2020-06-23 17:47

嗯嗯 已经实现了 我做完了所有的exercises 这是我自己的答案 https://github.com/master-davidlee/rustlings_answer

--
👇
whfuyn: 哇噢,这个项目有意思啊,我都不知道有这么个东西。 这个题目我看了半天才看明白它是啥意思,还以为是说要写个数字到字母的成绩表示。 其实在你把grade写成泛型T以后,直接运行会看到报错:

error[E0277]: `T` doesn't implement `std::fmt::Display`
  --> exercises/generics/generics3.rs:18:92
   |
18 |         format!("{} ({}) - achieved a grade of {}", &self.student_name, &self.student_age, &self.grade)
   |                                                                                            ^^^^^^^^^^^ `T` cannot be formatted with the default formatter
   |
   = help: the trait `std::fmt::Display` is not implemented for `T`
   = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
help: consider restricting this type parameter with `T: std::fmt::Display`
  --> exercises/generics/generics3.rs:16:6
   |
16 | impl<T> ReportCard<T> {
   |      ^
   = note: required because of the requirements on the impl of `std::fmt::Display` for `&T`
   = note: required by `std::fmt::Display::fmt`
   = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.

其中的帮助说的挺清楚的了help: consider restricting this type parameter with \T: std::fmt::Display``,你根本不需要为T实现Display,你只需要把T限制为已经实现Display的类型。

作者 newbie 2020-06-23 17:46

嗯嗯 下次注意

--
👇
Mike Tang: 这样贴效果不好,应该抄过来

gwy15 2020-06-23 15:19

https://github.com/gwy15/rustlings/commit/c4554b48fccdc612d935cafbe71124a153904d83#diff-5ece68a5c6fc6606e542676d78b6c051

whfuyn 2020-06-23 15:07

哇噢,这个项目有意思啊,我都不知道有这么个东西。 这个题目我看了半天才看明白它是啥意思,还以为是说要写个数字到字母的成绩表示。 其实在你把grade写成泛型T以后,直接运行会看到报错:

error[E0277]: `T` doesn't implement `std::fmt::Display`
  --> exercises/generics/generics3.rs:18:92
   |
18 |         format!("{} ({}) - achieved a grade of {}", &self.student_name, &self.student_age, &self.grade)
   |                                                                                            ^^^^^^^^^^^ `T` cannot be formatted with the default formatter
   |
   = help: the trait `std::fmt::Display` is not implemented for `T`
   = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
help: consider restricting this type parameter with `T: std::fmt::Display`
  --> exercises/generics/generics3.rs:16:6
   |
16 | impl<T> ReportCard<T> {
   |      ^
   = note: required because of the requirements on the impl of `std::fmt::Display` for `&T`
   = note: required by `std::fmt::Display::fmt`
   = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.

其中的帮助说的挺清楚的了help: consider restricting this type parameter with \T: std::fmt::Display``,你根本不需要为T实现Display,你只需要把T限制为已经实现Display的类型。

Mike Tang 2020-06-23 15:05

这样贴效果不好,应该抄过来

1 共 6 条评论, 1 页