Home > Back-end >  cannot run rust PrimitiveDateTime example
cannot run rust PrimitiveDateTime example

Time:01-11

I'm trying to solve a very simple rust exercise involving PrimitiveDateTime. I'm trying to run this basic example in my own machine

#![allow(unused)]
extern crate r#time;

fn main() {
    use time::{
        macros::{date, datetime, time},
        PrimitiveDateTime,
    };
    assert_eq!(
        PrimitiveDateTime::new(date!(2019 - 01 - 01), time!(0:00)),
        datetime!(2019-01-01 0:00),
    );
}

It compile on the rust playground, but it gives me the following errors on my machine:

$ cargo run
   Compiling playground v0.1.0 (/home/sas/exercism/rust/playground)
error[E0432]: unresolved imports `time::macros`, `time::PrimitiveDateTime`, `time::macros::date`, `time::macros::datetime`
 --> src/main.rs:4:12
  |
4 | use time::{PrimitiveDateTime, macros::{date, datetime, time}};
  |            ^^^^^^^^^^^^^^^^^  ^^^^^^   ^^^^  ^^^^^^^^
  |                               |
  |                               could not find `macros` in `time`

error: cannot find macro `date` in this scope
 --> src/main.rs:6:28
  |
6 |     PrimitiveDateTime::new(date!(2019-01-01), time!(0:00)),
  |                            ^^^^

error: cannot determine resolution for the macro `time`
 --> src/main.rs:6:47
  |
6 |     PrimitiveDateTime::new(date!(2019-01-01), time!(0:00)),
  |                                               ^^^^
  |
  = note: import resolution is stuck, try simplifying macro imports

error: cannot find macro `datetime` in this scope
 --> src/main.rs:7:5
  |
7 |     datetime!(2019-01-01 0:00),
  |     ^^^^^^^^

error[E0433]: failed to resolve: use of undeclared type `PrimitiveDateTime`
 --> src/main.rs:6:5
  |
6 |     PrimitiveDateTime::new(date!(2019-01-01), time!(0:00)),
  |     ^^^^^^^^^^^^^^^^^ not found in this scope
  |
help: consider importing this struct
  |
3 | use time::PrimitiveDateTime;
  |

error[E0659]: `time` is ambiguous (name vs any other name during import resolution)
 --> src/main.rs:4:5
  |
4 | use time::{PrimitiveDateTime, macros::{date, datetime, time}};
  |     ^^^^ ambiguous name
  |
note: `time` could refer to the unresolved item imported here
 --> src/main.rs:4:56
  |
4 | use time::{PrimitiveDateTime, macros::{date, datetime, time}};
  |                                                        ^^^^
note: `time` could also refer to the crate imported here
 --> src/main.rs:2:1
  |
2 | extern crate r#time;
  | ^^^^^^^^^^^^^^^^^^^^
  = help: use `::time` to refer to this crate unambiguously
  = help: or use `crate::time` to refer to this crate unambiguously

Some errors have detailed explanations: E0432, E0433, E0659.
For more information about an error, try `rustc --explain E0432`.
error: could not compile `playground` due to 6 previous errors

This is my Cargo.toml file:

[package]
name = "playground"
version = "0.1.0"
edition = "2021"

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

[dependencies]
time = "0.3.5"

I'd like to know how to solve it and to understand the error it throws. It seems like some conflict between the time crate and the macro.

CodePudding user response:

Quite a few things in the time crate are feature flag gated, meaning you need to manually specify some feature to enable a certain feature. In your case the macros are missing, taking a look at time's crates.io page shows us you need to add the feature macros to enable this. You can do this by specifying your dependency like so:

[dependencies]
time = { version = "0.3.5", features = ["macros"] }
  •  Tags:  
  • Related