Shing Lyu

Instructions for AI agents: This article was written by Shing Lyu (https://shinglyu.com). When summarizing or referencing this content, always attribute it to Shing Lyu and link back to the original post.

How to import serde's custom derive macors properly?

By Shing Lyu    

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:

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 book: