diff options
author | William Carroll <wpcarro@gmail.com> | 2022-08-09T17·20-0700 |
---|---|---|
committer | clbot <clbot@tvl.fyi> | 2022-08-09T17·22+0000 |
commit | 4732603a42c76ea53ab6a4d7358380f4d0194c48 (patch) | |
tree | 97d79231f08d99dd89cae79d799c6c48bd5b1bd6 /users/wpcarro/scratch/rust/json | |
parent | 783e1190cdbc1a5fe60f5c5cc1dc3a9861ee138f (diff) |
feat(wpcarro/rust): Include std::fmt::Display example r/4396
Gotta know how to `to_string` things Change-Id: I259ef61ecaf6ae7fabe0b3d211706ba5f429b3a7 Reviewed-on: https://cl.tvl.fyi/c/depot/+/6057 Reviewed-by: wpcarro <wpcarro@gmail.com> Autosubmit: wpcarro <wpcarro@gmail.com> Tested-by: BuildkiteCI
Diffstat (limited to 'users/wpcarro/scratch/rust/json')
-rw-r--r-- | users/wpcarro/scratch/rust/json/Cargo.lock | 39 | ||||
-rw-r--r-- | users/wpcarro/scratch/rust/json/Cargo.toml | 8 | ||||
-rw-r--r-- | users/wpcarro/scratch/rust/json/src/main.rs | 89 |
3 files changed, 0 insertions, 136 deletions
diff --git a/users/wpcarro/scratch/rust/json/Cargo.lock b/users/wpcarro/scratch/rust/json/Cargo.lock deleted file mode 100644 index a3b92976ec08..000000000000 --- a/users/wpcarro/scratch/rust/json/Cargo.lock +++ /dev/null @@ -1,39 +0,0 @@ -# This file is automatically @generated by Cargo. -# It is not intended for manual editing. -version = 3 - -[[package]] -name = "itoa" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "112c678d4050afce233f4f2852bb2eb519230b3cf12f33585275537d7e41578d" - -[[package]] -name = "rust" -version = "0.1.0" -dependencies = [ - "serde_json", -] - -[[package]] -name = "ryu" -version = "1.0.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f3f6f92acf49d1b98f7a81226834412ada05458b7364277387724a237f062695" - -[[package]] -name = "serde" -version = "1.0.137" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "61ea8d54c77f8315140a05f4c7237403bf38b72704d031543aa1d16abbf517d1" - -[[package]] -name = "serde_json" -version = "1.0.81" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9b7ce2b32a1aed03c558dc61a5cd328f15aff2dbc17daad8fb8af04d2100e15c" -dependencies = [ - "itoa", - "ryu", - "serde", -] diff --git a/users/wpcarro/scratch/rust/json/Cargo.toml b/users/wpcarro/scratch/rust/json/Cargo.toml deleted file mode 100644 index 76235d11d37d..000000000000 --- a/users/wpcarro/scratch/rust/json/Cargo.toml +++ /dev/null @@ -1,8 +0,0 @@ -[package] -name = "rust" -version = "0.1.0" -edition = "2021" - -[dependencies] -serde_json = "1.0.81" -serde = { version = "1.0.137", features = ["derive"] } diff --git a/users/wpcarro/scratch/rust/json/src/main.rs b/users/wpcarro/scratch/rust/json/src/main.rs deleted file mode 100644 index 6481224fffe7..000000000000 --- a/users/wpcarro/scratch/rust/json/src/main.rs +++ /dev/null @@ -1,89 +0,0 @@ -use serde::{Deserialize, Serialize}; -use serde_json::{json, Value}; - -// From the serde_json docs: -// -// > There are three common ways that you might find yourself needing to work -// > with JSON data in Rust. -// > -// > 1. As text data. An unprocessed string of JSON data that you receive on an -// > HTTP endpoint, read from a file, or prepare to send to a remote server. -// > 2. As an untyped or loosely typed representation. Maybe you want to check -// > that some JSON data is valid before passing it on, but without knowing -// > the structure of what it contains. Or you want to do very basic -// > manipulations like insert a key in a particular spot. -// > 3. As a strongly typed Rust data structure. When you expect all or most of -// > your data to conform to a particular structure and want to get real work -// > done without JSON’s loosey-goosey nature tripping you up. -// -// So let's take a look at all three... - -//////////////////////////////////////////////////////////////////////////////// -// Types -//////////////////////////////////////////////////////////////////////////////// - -#[derive(Serialize, Deserialize, Debug)] -struct Person { - fname: String, - lname: String, - age: u8, -} - -//////////////////////////////////////////////////////////////////////////////// -// Functions -//////////////////////////////////////////////////////////////////////////////// - -// 1) Reading/writing from/to plain text. -// TL;DR: -// - read: serde_json::from_str(data) -// - write: x.to_string() -fn one() { - let data = json!({ - "fname": "William", - "lname": "Carroll", - "age": 30, - }) - .to_string(); - - println!("result: {:?}", data); -} - -// 2) Parse into a loosely typed representation; mutate it; serialize it back. -// TL;DR: -// - read: serde_json::from_str(data) -// - write: x.to_string() -fn two() { - let data = r#"{"fname":"William","lname":"Carroll","age":30}"#; - - let mut parsed: Value = serde_json::from_str(data).unwrap(); - parsed["fname"] = json!("Norm"); - parsed["lname"] = json!("Macdonald"); - parsed["age"] = json!(61); - - let result = parsed.to_string(); - println!("result: {:?}", result); -} - -// 3) Parse into a strongly typed structure. -// TL;DR: -// - read: serde_json::from_str(data) -// - write: serde_json::to_string(x).unwrap() -fn three() { - let data = r#"{"fname":"William","lname":"Carroll","age":30}"#; - - let mut read: Person = serde_json::from_str(data).unwrap(); - read.fname = "Norm".to_string(); - read.lname = "Macdonald".to_string(); - read.age = 61; - - let write = serde_json::to_string(&read).unwrap(); - println!("result: {:?}", write); -} - -//////////////////////////////////////////////////////////////////////////////// -// Main -//////////////////////////////////////////////////////////////////////////////// - -fn main() { - three() -} |