Search code examples
rustserde

Why do we have two ways to include serde_derive?


I am learning rust serde library now and want to understand is there any difference between these two ways to include Serialization / Deserialization features

What is the proper way (is using in production code)?

Example 1

# Cargo.toml

serde = { version = "1.0", features = ["derive"] }

I this case I have to use use serde::{Deserialize, Serialize}; )

Example 2

# Cargo.toml
[dependencies]
serde = {version="1.0.117"}
serde_derive = {version="1.0.117"}

I this case I have to use use serde_derive::{Deserialize, Serialize}; )


Solution

  • The common way is to include serde and enable the derive feature. This way, you only have one direct dependency, you don't need to worry about mismatch in serde and serde-derive versions, and you can use the derive macros directly from serde.