-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathmut_ref.rs
52 lines (43 loc) · 912 Bytes
/
mut_ref.rs
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
// Compiler:
//
// Run-time:
// stdout: 2
// 7
// 6
// 11
#![feature(no_core)]
#![no_std]
#![no_core]
#![no_main]
extern crate mini_core;
use mini_core::*;
struct Test {
field: isize,
}
fn test(num: isize) -> Test {
Test { field: num + 1 }
}
fn update_num(num: &mut isize) {
*num = *num + 5;
}
#[no_mangle]
extern "C" fn main(mut argc: isize, _argv: *const *const u8) -> i32 {
let mut test = test(argc);
unsafe {
libc::printf(b"%ld\n\0" as *const u8 as *const i8, test.field);
}
update_num(&mut test.field);
unsafe {
libc::printf(b"%ld\n\0" as *const u8 as *const i8, test.field);
}
update_num(&mut argc);
unsafe {
libc::printf(b"%ld\n\0" as *const u8 as *const i8, argc);
}
let refe = &mut argc;
*refe = *refe + 5;
unsafe {
libc::printf(b"%ld\n\0" as *const u8 as *const i8, argc);
}
0
}