summaryrefslogtreecommitdiff
path: root/meap/meap-code/ch6/ch6-memscan-3/src
diff options
context:
space:
mode:
authorAdam Carpenter <gitlab@53hor.net>2019-07-09 15:14:04 -0400
committerAdam Carpenter <gitlab@53hor.net>2019-07-09 15:14:04 -0400
commit7e8ee5ed9cad6484e9f13f81731b102ced58402e (patch)
tree5395402ab07bbb5a659dbd68c701e22a1227202f /meap/meap-code/ch6/ch6-memscan-3/src
downloadlearning-rust-7e8ee5ed9cad6484e9f13f81731b102ced58402e.tar.xz
learning-rust-7e8ee5ed9cad6484e9f13f81731b102ced58402e.zip
Init.
Diffstat (limited to 'meap/meap-code/ch6/ch6-memscan-3/src')
-rwxr-xr-xmeap/meap-code/ch6/ch6-memscan-3/src/main.rs21
1 files changed, 21 insertions, 0 deletions
diff --git a/meap/meap-code/ch6/ch6-memscan-3/src/main.rs b/meap/meap-code/ch6/ch6-memscan-3/src/main.rs
new file mode 100755
index 0000000..f25dfb5
--- /dev/null
+++ b/meap/meap-code/ch6/ch6-memscan-3/src/main.rs
@@ -0,0 +1,21 @@
+static GLOBAL: i32 = 1000; // <1> Create a global static, which is a global variable in Rust programs
+
+fn noop() -> *const i32 {
+ let noop_local = 12345; // <2> Create a local variable within noop(). While an optimizer might clear this away, hopefully we r
+ &noop_local as *const i32 // <3> Return the address of `noop_local` as a raw pointer
+}
+
+fn main() {
+ let local_str = "a"; // <4>
+ let local_int = 123; // <4>
+ let boxed_str = Box::new('b'); // <4>
+ let boxed_int = Box::new(789); // <4>
+ let fn_int = noop(); // <4> Create various values of several types, including values on the heap.
+
+ println!("GLOBAL: {:p}", &GLOBAL as *const i32); // <5>
+ println!("local_str: {:p}", local_str as *const str); // <5>
+ println!("local_int: {:p}", &local_int as *const i32); // <5>
+ println!("boxed_int: {:p}", Box::into_raw(boxed_int)); // <5>
+ println!("boxed_str: {:p}", Box::into_raw(boxed_str)); // <5>
+ println!("fn_int: {:p}", fn_int); // <5> Print out the values' addresses
+}