From db9cb70d5d7807adff255e21a04d78c5700d1260 Mon Sep 17 00:00:00 2001 From: William Carroll Date: Thu, 1 Sep 2022 09:53:00 -0700 Subject: [PATCH] feat(wpcarro/rust): Define Rc example My foray into "smart pointer" land. Change-Id: I4ca775c72168dd34d90bf88fa41149867cd7fdae Reviewed-on: https://cl.tvl.fyi/c/depot/+/6244 Tested-by: BuildkiteCI Reviewed-by: wpcarro Autosubmit: wpcarro --- users/wpcarro/scratch/rust/shell.nix | 2 +- users/wpcarro/scratch/rust/src/main.rs | 3 ++- users/wpcarro/scratch/rust/src/rc/mod.rs | 12 ++++++++++++ 3 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 users/wpcarro/scratch/rust/src/rc/mod.rs diff --git a/users/wpcarro/scratch/rust/shell.nix b/users/wpcarro/scratch/rust/shell.nix index 064e7d8bb..98e2dbf4b 100644 --- a/users/wpcarro/scratch/rust/shell.nix +++ b/users/wpcarro/scratch/rust/shell.nix @@ -1,4 +1,4 @@ -{ pkgs, ... }: +{ pkgs ? import { }, ... }: pkgs.mkShell { buildInputs = [ diff --git a/users/wpcarro/scratch/rust/src/main.rs b/users/wpcarro/scratch/rust/src/main.rs index 185e7236c..671b33093 100644 --- a/users/wpcarro/scratch/rust/src/main.rs +++ b/users/wpcarro/scratch/rust/src/main.rs @@ -3,6 +3,7 @@ use serde_json::{json, Value}; mod display; mod json; +mod rc; mod stdin; //////////////////////////////////////////////////////////////////////////////// @@ -10,5 +11,5 @@ mod stdin; //////////////////////////////////////////////////////////////////////////////// fn main() { - stdin::example(); + rc::example(); } diff --git a/users/wpcarro/scratch/rust/src/rc/mod.rs b/users/wpcarro/scratch/rust/src/rc/mod.rs new file mode 100644 index 000000000..67251ca6a --- /dev/null +++ b/users/wpcarro/scratch/rust/src/rc/mod.rs @@ -0,0 +1,12 @@ +// Playing around with Rust's "smart pointers". Starting off with a wrapper type +// that allows multiple readers (owners?) of some data. + +use std::rc::Rc; + +pub fn example() { + let five = Rc::new(5); + let x = Rc::clone(&five); + let y = Rc::clone(&five); + let z = Rc::clone(&five); + println!("result: {}", *x + *y + *z) +}