diff options
author | Adam Carpenter <53hornet@gmail.com> | 2019-03-21 17:10:15 -0400 |
---|---|---|
committer | Adam Carpenter <53hornet@gmail.com> | 2019-03-21 17:10:15 -0400 |
commit | 333965318a5d000140d54be2a2a9e0832372f271 (patch) | |
tree | e7f28b53144982cfdf2cb95c9d71e5ea7a566b77 /advanced/unsafe-rust/src | |
parent | 2fadb006603139c335ed8df8fa9c25402986c4fd (diff) | |
download | learning-rust-333965318a5d000140d54be2a2a9e0832372f271.tar.xz learning-rust-333965318a5d000140d54be2a2a9e0832372f271.zip |
Added advanced features.
Diffstat (limited to 'advanced/unsafe-rust/src')
-rw-r--r-- | advanced/unsafe-rust/src/main.rs | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/advanced/unsafe-rust/src/main.rs b/advanced/unsafe-rust/src/main.rs new file mode 100644 index 0000000..c9967a8 --- /dev/null +++ b/advanced/unsafe-rust/src/main.rs @@ -0,0 +1,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 +} + |