diff options
author | Adam Carpenter <53hornet@gmail.com> | 2019-03-27 15:32:37 -0400 |
---|---|---|
committer | Adam Carpenter <53hornet@gmail.com> | 2019-03-27 15:32:37 -0400 |
commit | 67cdcc2e12118becb823e20a40cc2687f2b8425a (patch) | |
tree | ed92c3234b89079e6d4cf36f5e80c5ffa79def48 /meap/meap-code/ch6/ch6-memscan-3 | |
parent | e25482fca375d318a39c3b54db396b0db6e0b263 (diff) | |
download | learning-rust-67cdcc2e12118becb823e20a40cc2687f2b8425a.tar.xz learning-rust-67cdcc2e12118becb823e20a40cc2687f2b8425a.zip |
Started Rust in Action MEAP.
Diffstat (limited to 'meap/meap-code/ch6/ch6-memscan-3')
-rw-r--r-- | meap/meap-code/ch6/ch6-memscan-3/Cargo.lock | 4 | ||||
-rw-r--r-- | meap/meap-code/ch6/ch6-memscan-3/Cargo.toml | 6 | ||||
-rw-r--r-- | meap/meap-code/ch6/ch6-memscan-3/src/main.rs | 21 |
3 files changed, 31 insertions, 0 deletions
diff --git a/meap/meap-code/ch6/ch6-memscan-3/Cargo.lock b/meap/meap-code/ch6/ch6-memscan-3/Cargo.lock new file mode 100644 index 0000000..b5ac3f7 --- /dev/null +++ b/meap/meap-code/ch6/ch6-memscan-3/Cargo.lock @@ -0,0 +1,4 @@ +[[package]] +name = "ch6-memscan-3" +version = "0.1.0" + diff --git a/meap/meap-code/ch6/ch6-memscan-3/Cargo.toml b/meap/meap-code/ch6/ch6-memscan-3/Cargo.toml new file mode 100644 index 0000000..39e2cc5 --- /dev/null +++ b/meap/meap-code/ch6/ch6-memscan-3/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "ch6-memscan-3" +version = "0.1.0" +authors = ["Tim McNamara <code@timmcnamara.co.nz>"] + +[dependencies] 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 100644 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 +} |