summaryrefslogtreecommitdiff
path: root/rust-book/advanced/unsafe-rust
diff options
context:
space:
mode:
authorAdam Carpenter <gitlab@53hor.net>2019-07-09 15:14:04 -0400
committerAdam Carpenter <gitlab@53hor.net>2019-07-09 15:14:04 -0400
commit7e8ee5ed9cad6484e9f13f81731b102ced58402e (patch)
tree5395402ab07bbb5a659dbd68c701e22a1227202f /rust-book/advanced/unsafe-rust
downloadlearning-rust-7e8ee5ed9cad6484e9f13f81731b102ced58402e.tar.xz
learning-rust-7e8ee5ed9cad6484e9f13f81731b102ced58402e.zip
Init.
Diffstat (limited to 'rust-book/advanced/unsafe-rust')
-rwxr-xr-xrust-book/advanced/unsafe-rust/Cargo.lock4
-rwxr-xr-xrust-book/advanced/unsafe-rust/Cargo.toml7
-rwxr-xr-xrust-book/advanced/unsafe-rust/src/main.rs62
3 files changed, 73 insertions, 0 deletions
diff --git a/rust-book/advanced/unsafe-rust/Cargo.lock b/rust-book/advanced/unsafe-rust/Cargo.lock
new file mode 100755
index 0000000..fbd902c
--- /dev/null
+++ b/rust-book/advanced/unsafe-rust/Cargo.lock
@@ -0,0 +1,4 @@
+[[package]]
+name = "unsafe-rust"
+version = "0.1.0"
+
diff --git a/rust-book/advanced/unsafe-rust/Cargo.toml b/rust-book/advanced/unsafe-rust/Cargo.toml
new file mode 100755
index 0000000..b37474a
--- /dev/null
+++ b/rust-book/advanced/unsafe-rust/Cargo.toml
@@ -0,0 +1,7 @@
+[package]
+name = "unsafe-rust"
+version = "0.1.0"
+authors = ["Adam Carpenter <53hornet@gmail.com>"]
+edition = "2018"
+
+[dependencies]
diff --git a/rust-book/advanced/unsafe-rust/src/main.rs b/rust-book/advanced/unsafe-rust/src/main.rs
new file mode 100755
index 0000000..c9967a8
--- /dev/null
+++ b/rust-book/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
+}
+