简体   繁体   中英

Rust: cannot find macro

I'm trying to run rust code from the postgres_types documentation.

The example code can be found here: postgres_types

my rust environment:

cargo --version cargo 1.40.0-nightly (5da4b4d47 2019-10-28)

rustc --version rustc 1.40.0-nightly (b520af6fd 2019-11-03)


main.rs

#[cfg(feature = "derive")]
use postgres_types::{ToSql, FromSql};

#[derive(Debug, ToSql, FromSql)]
enum Mood {
    Sad,
    Ok,
    Happy,
}

fn main() {
    let mood = Mood::Sad;

    println!("{:?}", mood);
}

Cargo.toml

...

[dependencies]
postgres-types = "0.1.0-alpha.1"

When I try and run with cargo run I get:

error: cannot find derive macro `ToSql` in this scope
 --> src\main.rs:4:17
  |
4 | #[derive(Debug, ToSql, FromSql)]
  |                 ^^^^^

error: cannot find derive macro `FromSql` in this scope
 --> src\main.rs:4:24
  |
4 | #[derive(Debug, ToSql, FromSql)]
  |                        ^^^^^^^

What am I doing wrong here? Clearly I'm missing something basic. Have I not imported the macro correctly?

Quoting from the documentation,

If the derive cargo feature is enabled, you can derive ToSql and FromSql implementations for custom Postgres types.

To enable the derive feature, you'll need to put this in Cargo.toml :

[dependencies]
postgres-types = {version = "0.1.0-alpha.1", features = ["derive"]}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM