How to import serde's custom derive macors properly?
Disclaimer: This content reflects my personal opinions, not those of any organizations I am or have been affiliated with. Code samples are provided for illustration purposes only, use with caution and test thoroughly before deployment.
TL;DR Follow the official documentation
A little bit of history:
- Before Rust 2018, you do:
extern crate serde;
#[macro_use] extern crate serde_derive; // Imports the procedural macros
#[derive(Serialize, Deserialize)]
struct Foo;
- But Rust 2018 introduced new way of doing this:
use serde_derive::{Serialize, Deserialize}; // Imports the procedural macros
#[derive(Serialize, Deserialize)]
struct Foo;
- However, the
serde
crate re-exports theserde_derive::{Serialize, Deserialize}
macros, hidden behind the feature flagderive
. So if you enable thederive
feature you can get both theSerialize
/Deserialize
traits and procedural macros (i.e., the custom derive) from theserde
crate by one singleuse
:
// Cargo.toml
[dependencies]
serde = { version = "1.0", features = ["derive"] }
// src/main.rs or lib.rs
use serde::{Serialize, Deserialize}; // Imports both the traits and procedural macros
#[derive(Serialize, Deserialize)]
struct Foo;
- You can choose not to use the
derive
feature. But you’ll run into this issue if any of your dependencies enables thederive
feature. You can either:- Just enable
derive
(recommended). - Use
serde::ser::Serialize
andserde::de::Deserialize
to get the trait and keep usingserde_derive::{Serialize, Deserialize}
to get the procedural macros.
- Just enable