小编Fee*_*Fee的帖子

如何从函数返回“impl Display”

我正在努力了解如何正确实现结构的任何类型的替代显示或特征。

\n
struct Fraction {\n  numerator: u32,\n  denominator: u32\n}\n\nimpl Fraction {\n  fn unicode(&self) -> impl Display {\n   // should I use named function or Closure?\n   // Where does f come from?\n   // can I re-use self?\n   // How can I implement a trait that has multiple required functions or `type Output =`?\n    fn fmt(&self, f: std::fmt::Formatter) -> std::fmt::Result {\n       write!("{}\xe2\x81\x84{}", self.numerator, self.denominator)\n    } // <- this returns `()`\n  }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

这不起作用,因为fn fmt- 作为函数定义 - 不会返回任何内容。使用未命名的闭包:

\n
impl Fraction …
Run Code Online (Sandbox Code Playgroud)

io traits rust

0
推荐指数
1
解决办法
67
查看次数

标签 统计

io ×1

rust ×1

traits ×1