As @Dogbert said, add serde = "1.0.136"
and serde_json = "1.0.79"
to the end of your Cargo.toml
(under "[dependencies]").
This will tell cargo to download the dependencies on the next cargo run
and use it from that point on.
Update two years later: I posed this question while starting to learn Rust and notice that there are multiple errors within the code, primarily the missing dependencies, so I am very thankful to the patient answerers and want to highlight cargo add serde
and cargo add serde_json
as recommended by @Kai.
Because the initial example is in such poor quality in hindsight here a working one for anyone landing here with a similar problem through the search function:
Working example with step by step guide
$ mkdir serdee
$ cd serdee
$ cargo init
$ cargo add serde serde_json
- Manually edit
Cargo.toml
and enable the derive feature:
[package]
name = "serdee"
version = "0.1.0"
edition = "2021"
[dependencies]
serde = { version = "1.0.204", features = ["derive"] }
serde_json = "1.0.120"
src/main.rs
use serde::Deserialize;
use std::fs::File;
use std::io::BufReader;
#[derive(Deserialize, Debug)]
struct Feed {
feed_url: String,
}
fn main() {
let file = File::open("./feed.json").expect("error opening feed file");
let reader = BufReader::new(file);
let feed: Feed = serde_json::from_reader(reader)
.expect("file contents should conform to the feed structure");
println!("{}", feed.feed_url);
}
feed.json
{ "feed_url": "http://testfeed.com/"}
serde_json
to yourCargo.toml
(along withserde
)?Cargo.toml
)