feat(wpcarro/rust): Define Rc<T> 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 <wpcarro@gmail.com>
Autosubmit: wpcarro <wpcarro@gmail.com>
This commit is contained in:
William Carroll 2022-09-01 09:53:00 -07:00 committed by clbot
parent e23a556873
commit db9cb70d5d
3 changed files with 15 additions and 2 deletions

View file

@ -1,4 +1,4 @@
{ pkgs, ... }:
{ pkgs ? import <nixpkgs> { }, ... }:
pkgs.mkShell {
buildInputs = [

View file

@ -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();
}

View file

@ -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)
}