about summary refs log tree commit diff
diff options
context:
space:
mode:
authorWilliam Carroll <wpcarro@gmail.com>2022-08-09T17·20-0700
committerclbot <clbot@tvl.fyi>2022-08-09T17·22+0000
commit4732603a42c76ea53ab6a4d7358380f4d0194c48 (patch)
tree97d79231f08d99dd89cae79d799c6c48bd5b1bd6
parent783e1190cdbc1a5fe60f5c5cc1dc3a9861ee138f (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
-rw-r--r--users/wpcarro/scratch/rust/Cargo.lock89
-rw-r--r--users/wpcarro/scratch/rust/Cargo.toml (renamed from users/wpcarro/scratch/rust/json/Cargo.toml)0
-rw-r--r--users/wpcarro/scratch/rust/json/Cargo.lock39
-rw-r--r--users/wpcarro/scratch/rust/src/display/mod.rs13
-rw-r--r--users/wpcarro/scratch/rust/src/json/mod.rs (renamed from users/wpcarro/scratch/rust/json/src/main.rs)14
-rw-r--r--users/wpcarro/scratch/rust/src/main.rs18
6 files changed, 123 insertions, 50 deletions
diff --git a/users/wpcarro/scratch/rust/Cargo.lock b/users/wpcarro/scratch/rust/Cargo.lock
new file mode 100644
index 000000000000..28aa1250cea4
--- /dev/null
+++ b/users/wpcarro/scratch/rust/Cargo.lock
@@ -0,0 +1,89 @@
+# 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 = "proc-macro2"
+version = "1.0.43"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0a2ca2c61bc9f3d74d2886294ab7b9853abd9c1ad903a3ac7815c58989bb7bab"
+dependencies = [
+ "unicode-ident",
+]
+
+[[package]]
+name = "quote"
+version = "1.0.21"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179"
+dependencies = [
+ "proc-macro2",
+]
+
+[[package]]
+name = "rust"
+version = "0.1.0"
+dependencies = [
+ "serde",
+ "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"
+dependencies = [
+ "serde_derive",
+]
+
+[[package]]
+name = "serde_derive"
+version = "1.0.137"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1f26faba0c3959972377d3b2d306ee9f71faee9714294e41bb777f83f88578be"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
+name = "serde_json"
+version = "1.0.81"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9b7ce2b32a1aed03c558dc61a5cd328f15aff2dbc17daad8fb8af04d2100e15c"
+dependencies = [
+ "itoa",
+ "ryu",
+ "serde",
+]
+
+[[package]]
+name = "syn"
+version = "1.0.99"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "58dbef6ec655055e20b86b15a8cc6d439cca19b667537ac6a1369572d151ab13"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "unicode-ident",
+]
+
+[[package]]
+name = "unicode-ident"
+version = "1.0.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c4f5b37a154999a8f3f98cc23a628d850e154479cd94decf3414696e12e31aaf"
diff --git a/users/wpcarro/scratch/rust/json/Cargo.toml b/users/wpcarro/scratch/rust/Cargo.toml
index 76235d11d37d..76235d11d37d 100644
--- a/users/wpcarro/scratch/rust/json/Cargo.toml
+++ b/users/wpcarro/scratch/rust/Cargo.toml
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/src/display/mod.rs b/users/wpcarro/scratch/rust/src/display/mod.rs
new file mode 100644
index 000000000000..838463109190
--- /dev/null
+++ b/users/wpcarro/scratch/rust/src/display/mod.rs
@@ -0,0 +1,13 @@
+use std::fmt;
+
+pub struct Person {
+    pub fname: String,
+    pub lname: String,
+    pub age: i8,
+}
+
+impl fmt::Display for Person {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        write!(f, "{}, {} ({} years old)", self.lname, self.fname, self.age)
+    }
+}
diff --git a/users/wpcarro/scratch/rust/json/src/main.rs b/users/wpcarro/scratch/rust/src/json/mod.rs
index 6481224fffe7..d3307b394ea4 100644
--- a/users/wpcarro/scratch/rust/json/src/main.rs
+++ b/users/wpcarro/scratch/rust/src/json/mod.rs
@@ -37,7 +37,7 @@ struct Person {
 //    TL;DR:
 //    - read:  serde_json::from_str(data)
 //    - write: x.to_string()
-fn one() {
+pub fn one() {
     let data = json!({
         "fname": "William",
         "lname": "Carroll",
@@ -52,7 +52,7 @@ fn one() {
 //    TL;DR:
 //    - read:  serde_json::from_str(data)
 //    - write: x.to_string()
-fn two() {
+pub fn two() {
     let data = r#"{"fname":"William","lname":"Carroll","age":30}"#;
 
     let mut parsed: Value = serde_json::from_str(data).unwrap();
@@ -68,7 +68,7 @@ fn two() {
 //    TL;DR:
 //    - read:  serde_json::from_str(data)
 //    - write: serde_json::to_string(x).unwrap()
-fn three() {
+pub fn three() {
     let data = r#"{"fname":"William","lname":"Carroll","age":30}"#;
 
     let mut read: Person = serde_json::from_str(data).unwrap();
@@ -79,11 +79,3 @@ fn three() {
     let write = serde_json::to_string(&read).unwrap();
     println!("result: {:?}", write);
 }
-
-////////////////////////////////////////////////////////////////////////////////
-// Main
-////////////////////////////////////////////////////////////////////////////////
-
-fn main() {
-    three()
-}
diff --git a/users/wpcarro/scratch/rust/src/main.rs b/users/wpcarro/scratch/rust/src/main.rs
new file mode 100644
index 000000000000..da9e3d3c637c
--- /dev/null
+++ b/users/wpcarro/scratch/rust/src/main.rs
@@ -0,0 +1,18 @@
+use serde::{Deserialize, Serialize};
+use serde_json::{json, Value};
+
+mod display;
+mod json;
+
+////////////////////////////////////////////////////////////////////////////////
+// Main
+////////////////////////////////////////////////////////////////////////////////
+
+fn main() {
+    let john: display::Person = display::Person {
+        fname: "John".to_string(),
+        lname: "Cleese".to_string(),
+        age: 82,
+    };
+    println!("Person: {}", john)
+}