From 7e8ee5ed9cad6484e9f13f81731b102ced58402e Mon Sep 17 00:00:00 2001 From: Adam Carpenter Date: Tue, 9 Jul 2019 15:14:04 -0400 Subject: Init. --- meap/ch6/src/main.rs | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100755 meap/ch6/src/main.rs (limited to 'meap/ch6/src') diff --git a/meap/ch6/src/main.rs b/meap/ch6/src/main.rs new file mode 100755 index 0000000..187cd7c --- /dev/null +++ b/meap/ch6/src/main.rs @@ -0,0 +1,89 @@ +//use std::borrow::Cow; +//use std::ffi::CStr; +//use std::os::raw::c_char; +// +//static B: [u8; 10] = [99, 97, 114, 114, 121, 116, 111, 119, 101, 108]; +//static C: [u8; 11] = [116, 104, 97, 110, 107, 115, 102, 105, 115, 104, 0]; +// +//fn main() { +// let a = 42; +// let b: String; +// let c: Cow; +// +// unsafe { +// let b_ptr = &B as *const u8 as *mut u8; +// b = String::from_raw_parts(b_ptr, 10, 10); +// +// let c_ptr = &C as *const u8 as *const c_char; +// c = CStr::from_ptr(c_ptr).to_string_lossy(); +// } +// +// println!("{} {} {}", a, b, c); +//} + + +//fn main() { +// let a: i64 = 42; +// let a_ptr = &a as *const i64; +// let a_addr: usize = unsafe { std::mem::transmute(a_ptr) }; +// +// println!("{} {:p} 0x{:x}", a, a_ptr, a_addr + 7); +//} + + +//fn main() { +// let ptr = 42 as *const Vec; +// +// unsafe { +// let new_addr = ptr.offset(4); +// println!("{:p} => {:p}", ptr, new_addr); +// } +//} + + +//fn main() { +// let password = String::from("lousy"); +// dbg!(is_strong(password)); +// dbg!(is_strong("breakage")); +//} +// +//fn is_strong>(password: T) -> bool { +// password.as_ref().len() > 5 +//} + + +//use std::mem::drop; +// +//fn main() { +//// let a: i32 = 40; +//// let b: Box = Box::new(60); +//// dbg!(a + *b); +// +// let a = Box::new(1); +// let b = Box::new(1); +// let c = Box::new(1); +// +// let result1 = *a + *b + *c; +// +// drop(a); +// let d = Box::new(1); +// let result2 = *b + *c + *d; +// +// println!("{} {}", result1, result2); +//} + + +fn main() { + let mut n_nonzero = 0; + + for i in 0..10_000 { + let ptr = i as *const u8; + let byte_at_addr = unsafe { *ptr }; + + if byte_at_addr != 0 { + n_nonzero += 1; + } + } + + println!("{}", n_nonzero); +} -- cgit v1.2.3