无法运行 rust PrimitiveDateTime 示例

ope*_*sas 1 datetime date rust rust-cargo

我正在尝试解决一个非常简单的 Rust 练习,涉及PrimitiveDateTime. 我正在尝试在我自己的机器上运行这个基本示例

#![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),
    );
}
Run Code Online (Sandbox Code Playgroud)

它在 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
Run Code Online (Sandbox Code Playgroud)

这是我的 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"
Run Code Online (Sandbox Code Playgroud)

我想知道如何解决它并了解它引发的错误。time板条箱和宏之间似乎存在一些冲突。

Min*_*ipe 5

板条箱中的很多东西time都是功能标志门控的,这意味着您需要手动指定某些功能才能启用某个功能。在您的情况下,宏丢失了,查看 的timecrates.io 页面会告诉我们您需要添加该功能macros才能启用此功能。您可以通过指定您的依赖项来做到这一点,如下所示:

[dependencies]
time = { version = "0.3.5", features = ["macros"] }
Run Code Online (Sandbox Code Playgroud)