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
serdecrate re-exports theserde_derive::{Serialize, Deserialize}macros, hidden behind the feature flagderive. So if you enable thederivefeature you can get both theSerialize/Deserializetraits and procedural macros (i.e., the custom derive) from theserdecrate 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
derivefeature. But you’ll run into this issue if any of your dependencies enables thederivefeature. You can either:- Just enable
derive(recommended). - Use
serde::ser::Serializeandserde::de::Deserializeto get the trait and keep usingserde_derive::{Serialize, Deserialize}to get the procedural macros.
- Just enable