Shing Lyu

How to import serde's custom derive macors properly?

By Shing Lyu    

Disclaimer: The views expressed here are my own; they do not reflect the views of my current and past employers.

TL;DR Follow the official documentation

A little bit of history:

extern crate serde;
#[macro_use] extern crate serde_derive; // Imports the procedural macros

#[derive(Serialize, Deserialize)]
struct Foo;
use serde_derive::{Serialize, Deserialize}; // Imports the procedural macros

#[derive(Serialize, Deserialize)]
struct Foo;
// 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;

Want to learn Rust? Check out my books:

Also available on O'Reilly and Amazon