diff options
author | William Carroll <wpcarro@gmail.com> | 2022-06-27T00·59-0700 |
---|---|---|
committer | clbot <clbot@tvl.fyi> | 2022-06-27T16·25+0000 |
commit | 8e9bfaba4743394d7620a6f61c23c2d2fd30b5e8 (patch) | |
tree | d7cedc8ea02d363d61c527d508a80550e3278959 | |
parent | 95f302f95da11bc6e891ae2ab675c43ce0167ad7 (diff) |
feat(wpcarro/rust): Show 1/3 json examples r/4257
See json/src/main.rs Change-Id: I83ee6b762b741a05932832a9a87eb8161bb95d0f Reviewed-on: https://cl.tvl.fyi/c/depot/+/5896 Reviewed-by: wpcarro <wpcarro@gmail.com> Autosubmit: wpcarro <wpcarro@gmail.com> Tested-by: BuildkiteCI
-rw-r--r-- | users/wpcarro/scratch/rust/json/Cargo.toml | 7 | ||||
-rw-r--r-- | users/wpcarro/scratch/rust/json/src/main.rs | 37 |
2 files changed, 44 insertions, 0 deletions
diff --git a/users/wpcarro/scratch/rust/json/Cargo.toml b/users/wpcarro/scratch/rust/json/Cargo.toml new file mode 100644 index 000000000000..ff4ed61e85e2 --- /dev/null +++ b/users/wpcarro/scratch/rust/json/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "rust" +version = "0.1.0" +edition = "2021" + +[dependencies] +serde_json = "1.0.81" diff --git a/users/wpcarro/scratch/rust/json/src/main.rs b/users/wpcarro/scratch/rust/json/src/main.rs new file mode 100644 index 000000000000..b810cfa77fe6 --- /dev/null +++ b/users/wpcarro/scratch/rust/json/src/main.rs @@ -0,0 +1,37 @@ +use serde_json::json; + +// 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... + +// 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); +} + +fn main() { + one() +} |