无法运行rust PrimitiveDateTime示例



我正试图解决一个涉及PrimitiveDateTime的非常简单的rust练习。我正在尝试在我自己的机器中运行这个基本示例

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

它在铁锈操场上编译,但它在我的机器上给了我以下错误:

$ 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机箱中有相当多的东西是功能标志门控的,这意味着您需要手动指定一些功能来启用某个功能。在您的情况下,宏丢失,请查看time的crates.io页面,了解您需要添加功能macros才能启用此功能。您可以这样指定您的依赖项:

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

相关内容

  • 没有找到相关文章

最新更新