![](/img/trans.png)
[英]How to get an implementation of fmt::Debug for a Vec of dyn Trait objects?
[英]How to implement a custom 'fmt::Debug' trait?
我猜你做的事情是这样的:
extern crate uuid;
use uuid::Uuid;
use std::fmt::Formatter;
use std::fmt::Debug;
#[derive(Debug)]
struct BlahLF {
id: Uuid,
}
impl BlahLF {
fn new() -> BlahLF {
return BlahLF { id: Uuid::new_v4() };
}
}
impl Debug for BlahLF {
fn fmt(&self, &mut f: Formatter) -> Result {
write!(f.buf, "Hi: {}", self.id);
}
}
...但是尝试实现此特征会产生:
error[E0243]: wrong number of type arguments
--> src/main.rs:19:41
|
19 | fn fmt(&self, &mut f: Formatter) -> Result {
| ^^^^^^ expected 2 type arguments, found 0
但是,这似乎是其他实现的方式。 我究竟做错了什么?
根据std::fmt
docs的例子:
extern crate uuid;
use uuid::Uuid;
use std::fmt;
struct BlahLF {
id: Uuid,
}
impl fmt::Debug for BlahLF {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Hi: {}", self.id)
}
}
要强调的部分是fmt::
in fmt::Result
。 没有它,你指的是普通的Result
类型。 平原Result
类型确实有两个泛型类型参数, fmt::Result
却没有。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.