不能 运行 生锈 PrimitiveDateTime 示例

cannot run rust PrimitiveDateTime example

我正在尝试解决涉及 PrimitiveDateTime 的非常简单的 Rust 练习。我正在尝试 运行 这个 basic example 在我自己的机器上

#![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),
    );
}

它在 rust playground, 上编译,但在我的机器上出现以下错误:

$ 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

这是我的 Cargo.toml 文件:

[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"

我想知道如何解决它并了解它抛出的错误。 time 板条箱和宏之间似乎有些冲突。

time crate 中的很多东西都是功能标志门控,这意味着您需要手动指定某些功能才能启用某个功能。在你的情况下宏丢失了,看看 time's crates.io page 告诉我们你需要添加功能 macros 来启用它。您可以像这样指定您的依赖项来做到这一点:

[dependencies]
time = { version = "0.3.5", features = ["macros"] }