DateTime<Utc> and serde Deserialize compilation errors

Since I no longer have to commute to work given the current circumstances with Covid-19, I have decided to invest some time to learn Rust.

Unfortunately, it seems I got stuck on something quite basic that I can't seem to figure out by myself. I am trying to use serde to deserialize an RFC3339 formatted string to a struct's field. This is the error I am seeing. Any help would be much appreciated.

Cargo.toml

[package]
name = "helloworld"
version = "0.1.0"
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
chrono = { version = "0.4.11", feaures = ["serde"] }
serde = { version = "1.0", features = ["derive"] }

src/lib.rs

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

#[derive(Deserialize, Serialize)]
struct Card {
    created_at: DateTime<Utc>,
}

The result of cargo build:

warning: unused manifest key: dependencies.chrono.feaures
   Compiling foo v0.1.0 (/Users/bittercold/dev/foo)
error[E0277]: the trait bound `chrono::datetime::DateTime<chrono::offset::utc::Utc>: _IMPL_SERIALIZE_FOR_Card::_serde::Deserialize<'_>` is not satisfied
 --> src/lib.rs:6:5
  |
6 |     created_at: DateTime<Utc>,
  |     ^^^^^^^^^^ the trait `_IMPL_SERIALIZE_FOR_Card::_serde::Deserialize<'_>` is not implemented for `chrono::datetime::DateTime<chrono::offset::utc::Utc>`
  |
  = note: required by `_IMPL_SERIALIZE_FOR_Card::_serde::de::SeqAccess::next_element`

error[E0277]: the trait bound `chrono::datetime::DateTime<chrono::offset::utc::Utc>: _IMPL_SERIALIZE_FOR_Card::_serde::Deserialize<'_>` is not satisfied
 --> src/lib.rs:6:5
  |
6 |     created_at: DateTime<Utc>,
  |     ^^^^^^^^^^ the trait `_IMPL_SERIALIZE_FOR_Card::_serde::Deserialize<'_>` is not implemented for `chrono::datetime::DateTime<chrono::offset::utc::Utc>`
  |
  = note: required by `_IMPL_SERIALIZE_FOR_Card::_serde::de::MapAccess::next_value`

error[E0277]: the trait bound `chrono::datetime::DateTime<chrono::offset::utc::Utc>: _IMPL_SERIALIZE_FOR_Card::_serde::Serialize` is not satisfied
 --> src/lib.rs:6:5
  |
6 |     created_at: DateTime<Utc>,
  |     ^^^^^^^^^^ the trait `_IMPL_SERIALIZE_FOR_Card::_serde::Serialize` is not implemented for `chrono::datetime::DateTime<chrono::offset::utc::Utc>`
  |
  = note: required by `_IMPL_SERIALIZE_FOR_Card::_serde::ser::SerializeStruct::serialize_field`

error: aborting due to 3 previous errors

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

To learn more, run the command again with --verbose.

Does this compile in your project?

use chrono::serde::ts_seconds;
#[derive(Deserialize, Serialize)]
struct S {
    #[serde(with = "ts_seconds")]
    time: DateTime<Utc>
}

features is spelled wrong on this line:

(It took a lot of experimenting before I noticed this; Cargo should really warn about unused keys like this...)

3 Likes

God damn, I was feeling pretty confused about this too.

Ah, there was a warning from cargo, but I didn't notice it above the much larger error message from rustc:

warning: unused manifest key: dependencies.chrono.feaures
1 Like

:man_facepalming:Thank you all for your help. I can confirm that fixing the fearues typo to features fixes the problem.

Perhaps Cargo should make some of its errors a bit noisier?

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.