aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/rust/compile/issue-850.rs
blob: e42b5573d1d169fbc22f034ff432e45aeef539e2 (plain)
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
#[lang = "sized"]
pub trait Sized {}

extern "C" {
    fn printf(s: *const i8, ...);
}

mod option {
    enum Option<T> {
        #[lang = "None"]
        None,
        #[lang = "Some"]
        Some(T),
    }
}

pub use option::Option::{self, None, Some};

fn divide(numerator: f64, denominator: f64) -> Option<f64> {
    if denominator == 0.0 {
        None
    } else {
        Some(numerator / denominator)
    }
}

fn main() {
    let result = divide(2.0, 3.0);

    match result {
        Some(x) => unsafe {
            let a = "Result: %i\n\0";
            let b = a as *const str;
            let c = b as *const i8;

            printf(c, x);
        },
        None => unsafe {
            let a = "Cannot divide by 0\n\0";
            let b = a as *const str;
            let c = b as *const i8;

            printf(c);
        },
    }
}