Skip to content

^ the trait Default is not implemented for chrono::Duration #1297

@akaNightmare

Description

@akaNightmare

Hello, received an error, and I assume the default for chrono::Duration should be Duration::zero(), anyway I tried to wrap Duration into my struct and implement Default trait for that and have no luck.
Thanks

use serde::{Deserialize, Serialize};
use chrono::Duration;

#[derive(Debug, Serialize, Deserialize)]
struct MyStruct {
    #[serde(skip)]
    duration: Duration,
    f: i32,
}

fn main() {
    // Create an instance of MyStruct
    let my_struct = MyStruct {
        duration: Duration::minutes(30),
        f: 12,
    };

    // Serialize it to JSON
    let serialized = serde_json::to_string(&my_struct).unwrap();
    println!("Serialized: {}", serialized);

    // Deserialize from JSON
    let deserialized: MyStruct = serde_json::from_str(&serialized).unwrap();
    println!("Deserialized: {:?}", deserialized);
}

The error:

error[E0277]: the trait bound `chrono::Duration: Default` is not satisfied
 --> src/main.rs:6:5
  |
6 |     #[serde(skip)]
  |     ^ the trait `Default` is not implemented for `chrono::Duration`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions