Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Project with serde cannot compile

Tags:

rust

serde

When I try to run the example from the serde repository:

#![feature(proc_macro)]

#[macro_use]
extern crate serde_derive;

extern crate serde_json;

#[derive(Serialize, Deserialize, Debug)]
struct Point {
    x: i32,
    y: i32,
}

fn main() {
    let point = Point { x: 1, y: 2 };

    // Convert the Point to a JSON string.
    let serialized = serde_json::to_string(&point).unwrap();

    // Prints serialized = {"x":1,"y":2}
    println!("serialized = {}", serialized);

    // Convert the JSON string back to a Point.
    let deserialized: Point = serde_json::from_str(&serialized).unwrap();

    // Prints deserialized = Point { x: 1, y: 2 }
    println!("deserialized = {:?}", deserialized);
}

I get an error:

error: failed to run rustc to learn about target-specific information

Caused by: process didn't exit successfully: rustc - --crate-name _ --print=file-names --crate-type bin --crate-type proc-macro --crate-type rlib --target x86_64-unknown-linux-gnu (exit code: 101) --- stderr error: unknown crate type: proc-macro

My Rust version is 1.13.0 and my Cargo.toml has these dependencies:

[dependencies]
serde = "*"
serde_derive = "*"

Should I use other dependencies or extra configuration?

like image 681
Antonio Romero Oca Avatar asked Jul 04 '26 17:07

Antonio Romero Oca


1 Answers

The #![feature(...)] attribute indicates code that uses Rust features which have not been stabilized yet. At the time the question was asked, the proc_macro feature was not yet stable. Serde needs this feature for its #[derive(Serialize, Deserialize)] macros.

Custom derives have been stabilized as of Rust 1.15 so the code in the question (with the feature attribute removed) should work on any Rust compiler since that version.

like image 148
dtolnay Avatar answered Jul 11 '26 18:07

dtolnay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!