首页 > 解决方案 > 如何定义具有不同时区的 chrono::DateTime 向量?

问题描述

我正在使用该chrono库,并且不知道如何定义一个结构字段,该字段是DateTime<Tz: TimeZone>具有TimeZone特征而不是具体类型的结构向量。

即,我可以这样做:

use chrono::{DateTime, TimeZone, Utc}; // 0.4.11

pub struct ItemSchedule {
    pub times: Vec<Box<DateTime<Utc>>>,
}

当我尝试使用特征对象时:

use chrono::{DateTime, TimeZone, Utc}; // 0.4.11

pub struct ItemSchedule {
    pub times: Vec<Box<DateTime<dyn TimeZone>>>,
}

我收到一个错误:

error[E0191]: the value of the associated type `Offset` (from trait `chrono::offset::TimeZone`) must be specified
 --> src/lib.rs:4:37
  |
4 |     pub times: Vec<Box<DateTime<dyn TimeZone>>>,
  |                                     ^^^^^^^^ help: specify the associated type: `TimeZone<Offset = Type>`

查看代码,TimeZone特征有一个名为 的关联类型Offset,它必须实现一个称为 的特征Offset。如果我尝试指定此类型声明,我也会收到错误消息。

标签: rust

解决方案


推荐阅读