[Rust 迷局] 之 method not found in `std::result::Result<std::string::String, CreateError>

yufei       3 年, 1 月 前       789

学习 Rust 的过程真辛苦,有些错误真的很违反常识,比如我有一个函数

enum CreateError;

hello() -> Result<String,Error> {}

然后调用的时候使用了 unwrap()

hello.unwrap();

但是一直提示错误

method not found in `std::result::Result<std::string::String, CreateError>

问题出在哪里呢?CreateError 没有注解 fmt::Debug 接口

这个问题我找了3个小时了,才发现问题出在这里,惭愧啊

解决方案如下

#[derive(Debug)]
enum CreateError;

hello() -> Result<String,Error> {}

根本原因是 Result::unwrap()E 接口的定义,E 要实现 Debug

impl<T, E> Result<T, E> 
    where E: Debug
目前尚无回复
简单教程 = 简单教程,简单编程
简单教程 是一个关于技术和学习的地方
现在注册
已注册用户请 登入
关于   |   FAQ   |   我们的愿景   |   广告投放   |  博客

  简单教程,简单编程 - IT 入门首选站

Copyright © 2013-2022 简单教程 twle.cn All Rights Reserved.