From 724cb4f09fc721c2a9427e4821b9be7e1709601e Mon Sep 17 00:00:00 2001 From: jburnett Date: Thu, 13 Jul 2023 21:03:02 -0400 Subject: [PATCH 1/4] Added struct ownership example --- examples/struct-ownership.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 examples/struct-ownership.rs diff --git a/examples/struct-ownership.rs b/examples/struct-ownership.rs new file mode 100644 index 0000000..c27cedf --- /dev/null +++ b/examples/struct-ownership.rs @@ -0,0 +1,33 @@ + +// This structure contains an 64-bit integer. The entire struct can be stored on the stack. +struct NoHeapElements { + val: i64, +} + + +// Rust offers `str` which can be on the stack and `String` which is allocated on the heap. Think of +// Rust's `str` as a fixed length char array, and `String` as a dynamically sized char array. +// This structure contains a String which is allocated on the heap. An instance of this struct may be +// on the stack and `desc` is a reference to the heap-allocated String. +struct AllHeapElements { + desc: String, +} + + +fn main () { + + // A struct that does not use the heap + let n = NoHeapElements { val: 74 }; + println!("\nThe variable `n` is type NoHeapElements"); + println!("`n` and elements of NoHeapElements are on the stack"); + println!("n.val: {}", n.val); + + let a = AllHeapElements { + desc: "String (not str) is always on the heap".to_string(), + }; + println!("\nThe variable `a` is type AllHeapElements"); + println!("`a` is on the stack; elements of AllHeapElements are on the heap"); + println!("a.desc = {}", a.desc); + + println!(); +} \ No newline at end of file From dd3aa300b19342aa9a5c67e69f4c971786836eb7 Mon Sep 17 00:00:00 2001 From: jburnett Date: Thu, 13 Jul 2023 22:41:52 -0400 Subject: [PATCH 2/4] Minor change to struct-ownership to validate GHA --- examples/struct-ownership.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/examples/struct-ownership.rs b/examples/struct-ownership.rs index c27cedf..2c8b64e 100644 --- a/examples/struct-ownership.rs +++ b/examples/struct-ownership.rs @@ -1,3 +1,6 @@ +/* + struct-ownership example + */ // This structure contains an 64-bit integer. The entire struct can be stored on the stack. struct NoHeapElements { From 6ae93789a3f26725acddcc8d7ad51c5bcd96e1c0 Mon Sep 17 00:00:00 2001 From: jburnett Date: Thu, 13 Jul 2023 22:46:51 -0400 Subject: [PATCH 3/4] Minor edit to verify GH workflow --- examples/struct-ownership.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/examples/struct-ownership.rs b/examples/struct-ownership.rs index 2c8b64e..496b5c5 100644 --- a/examples/struct-ownership.rs +++ b/examples/struct-ownership.rs @@ -1,4 +1,6 @@ /* + AltaModa Technologies, LLC + struct-ownership example */ From 8adc4aa624d885e935412d274858d3a27d2e0c2b Mon Sep 17 00:00:00 2001 From: jburnett Date: Thu, 13 Jul 2023 23:56:19 -0400 Subject: [PATCH 4/4] Move ownership comments for struct-ownership --- examples/struct-ownership.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/examples/struct-ownership.rs b/examples/struct-ownership.rs index 496b5c5..3c68ee6 100644 --- a/examples/struct-ownership.rs +++ b/examples/struct-ownership.rs @@ -1,6 +1,6 @@ /* AltaModa Technologies, LLC - + struct-ownership example */ @@ -34,5 +34,12 @@ fn main () { println!("`a` is on the stack; elements of AllHeapElements are on the heap"); println!("a.desc = {}", a.desc); + // This code fails to compile because `a2` has taken ownership of + // the struct reference from `a`. Rust refers to this as + // ownership _moving_ from `a` to `a2`. Uncommenting these lines + // see the "borrow after move" error on compilation. + // let a2 = a; + // println!("a.desc = {}", a.desc); + println!(); } \ No newline at end of file