summaryrefslogtreecommitdiff
path: root/meap/ch6/src/main.rs
blob: 187cd7c3592b1b9d009e2edeccb7841b29283ab7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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);
}