我正在努力了解如何正确实现结构的任何类型的替代显示或特征。
\nstruct 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}\nRun Code Online (Sandbox Code Playgroud)\n这不起作用,因为fn fmt- 作为函数定义 - 不会返回任何内容。使用未命名的闭包:
impl Fraction …Run Code Online (Sandbox Code Playgroud)