summaryrefslogtreecommitdiff
path: root/rust-book/advanced/unsafe-rust/src/main.rs
blob: c9967a8ebaef04e2073ed4936c67685bff9af515 (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
fn main() {
    let mut num = 5;

    let r1 = &num as *const i32;
    let r2 = &mut num as *mut i32;

    let address = 0x012345usize;
    let r = address as *const i32;

    unsafe {
        println!("{}", *r1);
        println!("{}", *r2);
    }

    unsafe {
        dangerous();
    }


    unsafe {
        println!("c call: {}", abs(-3));
    }


    unsafe {
        HELLO_WORLD = "test";
        println!("{}", HELLO_WORLD);
    }
    println!("{}", HI_THERE);

    add_to_count(3);

    unsafe { 
        println!("counter: {}", COUNTER);
    }
}

unsafe fn dangerous() {}

extern "C" {
    fn abs(input: i32) -> i32;
}

static mut HELLO_WORLD: &str = "hello world";
static mut COUNTER: u32 = 0;

fn add_to_count(inc: u32) {
    unsafe {
        COUNTER += inc;
    }
}

const HI_THERE: &str = "hi there";

unsafe trait Foo {
    // methods
}

unsafe impl Foo for i32 {
    // implementations
}