如何实现自定义'fmt :: Debug'特性?

 皇族灬柒诺彡_241 发布于 2023-01-19 14:46

我猜你做的事情是这样的:

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

但是,这似乎是其他实现的方式.我究竟做错了什么?

1 个回答
  • 根据std::fmt文档中的示例:

    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::fmt::Result.没有它,你指的是普通Result类型.普通Result类型确实有两个泛型类型参数,fmt::Result没有.

    2023-01-19 14:48 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有