rust - 不满足 `Default` 特征绑定(bind)在标准库所需的类型上

标签 rust traits typetraits web3

我正在使用 web3 crate 中的类型, web3::contract::Contract<web3::transports::Http> .编译器提示 E0277 :

$ cargo run
   Compiling proj v0.1.0 (/home/user/proj)
error[E0277]: the trait bound `web3::contract::Contract<web3::transports::Http>: Default` is not satisfied
  --> src/book.rs:38:5
   |
38 | /     #[serde(skip)]
39 | |     abi: web3::contract::Contract<OMETransport>,
   | |_______________________________________________^ the trait `Default` is not implemented for `web3::contract::Contract<web3::transports::Http>`
   |
   = note: required by `std::default::Default::default`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.
error: could not compile `proj`

To learn more, run the command again with --verbose.
这个错误怎么可能来自 std::default::Default::default ?
相关类型(即包含 web3 类型的结构)的定义是:
/// Represents an order book for a particular market
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Book {
    market: Address, /* the address of the market */
    bids: BTreeMap<U256, VecDeque<Order>>, /* buy-side */
    asks: BTreeMap<U256, VecDeque<Order>>, /* sell-side */
    #[serde(serialize_with = "from_hex_se", deserialize_with = "from_hex_de")]
    ltp: U256, /* last traded price */
    depth: (usize, usize), /* depth  */
    crossed: bool,   /* is book crossed? */
    #[serde(serialize_with = "from_hex_se", deserialize_with = "from_hex_de")]
    spread: U256, /* bid-ask spread */
    #[serde(skip)]
    abi: web3::contract::Contract<web3::transports:Http>,
}

最佳答案

serde documentation对于 #[serde(skip)]说:

When deserializing, Serde will use Default::default() or the function given by default = "..." to get a default value for this field.

关于rust - 不满足 `Default` 特征绑定(bind)在标准库所需的类型上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65930855/

相关文章:

c++ - 有没有办法检测混合类型和非类型的任意模板类?

rust - 将引用分配给不同大小的数组

Rust 声明先分配后模式

rust - 用println打印一个字符多次

rust - 如何在 Rust 中定义一个符合 trait 的变量?

c++ - 我们需要 std::as_const() 做什么?

C++:在模板化类型中存在命名成员时在模板化类中提供类函数?

rust - 处理可能是驱动器号或连接到其他路径的路径的正确方法是什么?

php - PHP 中的特征是否受 namespace 影响?

rust - 如何为实现特定特征的所有类型批量实现反序列化?