diff options
author | Adam Carpenter <gitlab@53hor.net> | 2019-07-09 15:14:04 -0400 |
---|---|---|
committer | Adam Carpenter <gitlab@53hor.net> | 2019-07-09 15:14:04 -0400 |
commit | 7e8ee5ed9cad6484e9f13f81731b102ced58402e (patch) | |
tree | 5395402ab07bbb5a659dbd68c701e22a1227202f /meap/ch6/src | |
download | learning-rust-7e8ee5ed9cad6484e9f13f81731b102ced58402e.tar.xz learning-rust-7e8ee5ed9cad6484e9f13f81731b102ced58402e.zip |
Init.
Diffstat (limited to 'meap/ch6/src')
-rwxr-xr-x | meap/ch6/src/main.rs | 89 |
1 files changed, 89 insertions, 0 deletions
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<str>; +// +// 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<String>; +// +// 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<T: AsRef<str>>(password: T) -> bool { +// password.as_ref().len() > 5 +//} + + +//use std::mem::drop; +// +//fn main() { +//// let a: i32 = 40; +//// let b: Box<i32> = 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); +} |