summaryrefslogtreecommitdiff
path: root/meap/meap-code/ch3/globalerror/src/main.rs
blob: c3db4cb6a8fa736fc5d8ddd725b56682a1047706 (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
extern crate rand;
use ::rand::Rng;

static mut ERROR: isize = 0;

struct File;

#[allow(unused_variables)]
fn read(f: &File, save_to: Vec<u8>) -> usize {
    if rand::thread_rng().gen_weighted_bool(10_000) {
        unsafe {
            ERROR = 1;
        }
    }

    0 // <> Always read() 0 bytes  
}

#[allow(unused_mut)]
fn main() {
    let mut f = File;
    let mut buffer = vec![];

    read(&f, buffer);
    unsafe {
        if ERROR != 0 {
            panic!("An error has occurred!")
        }
    }
}