summaryrefslogtreecommitdiff
path: root/rust-book/advanced
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
downloadlearning-rust-7e8ee5ed9cad6484e9f13f81731b102ced58402e.tar.xz
learning-rust-7e8ee5ed9cad6484e9f13f81731b102ced58402e.zip
Init.
Diffstat (limited to 'rust-book/advanced')
-rwxr-xr-xrust-book/advanced/adv-fn-closure/Cargo.lock11
-rwxr-xr-xrust-book/advanced/adv-fn-closure/Cargo.toml8
-rwxr-xr-xrust-book/advanced/adv-fn-closure/src/main.rs79
-rwxr-xr-xrust-book/advanced/adv-lifetimes/Cargo.lock4
-rwxr-xr-xrust-book/advanced/adv-lifetimes/Cargo.toml7
-rwxr-xr-xrust-book/advanced/adv-lifetimes/src/main.rs36
-rwxr-xr-xrust-book/advanced/adv-traits/Cargo.lock4
-rwxr-xr-xrust-book/advanced/adv-traits/Cargo.toml7
-rwxr-xr-xrust-book/advanced/adv-traits/src/main.rs163
-rwxr-xr-xrust-book/advanced/adv-types/Cargo.lock4
-rwxr-xr-xrust-book/advanced/adv-types/Cargo.toml7
-rwxr-xr-xrust-book/advanced/adv-types/src/main.rs41
-rwxr-xr-xrust-book/advanced/hello_macro/Cargo.toml7
-rwxr-xr-xrust-book/advanced/hello_macro/src/lib.rs3
-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
17 files changed, 454 insertions, 0 deletions
diff --git a/rust-book/advanced/adv-fn-closure/Cargo.lock b/rust-book/advanced/adv-fn-closure/Cargo.lock
new file mode 100755
index 0000000..360607c
--- /dev/null
+++ b/rust-book/advanced/adv-fn-closure/Cargo.lock
@@ -0,0 +1,11 @@
+[[package]]
+name = "adv-fn-closure"
+version = "0.1.0"
+dependencies = [
+ "hello_macro 0.1.0",
+]
+
+[[package]]
+name = "hello_macro"
+version = "0.1.0"
+
diff --git a/rust-book/advanced/adv-fn-closure/Cargo.toml b/rust-book/advanced/adv-fn-closure/Cargo.toml
new file mode 100755
index 0000000..6cd8dbd
--- /dev/null
+++ b/rust-book/advanced/adv-fn-closure/Cargo.toml
@@ -0,0 +1,8 @@
+[package]
+name = "adv-fn-closure"
+version = "0.1.0"
+authors = ["Adam Carpenter <53hornet@gmail.com>"]
+edition = "2018"
+
+[dependencies]
+hello_macro = { path = "../hello_macro" }
diff --git a/rust-book/advanced/adv-fn-closure/src/main.rs b/rust-book/advanced/adv-fn-closure/src/main.rs
new file mode 100755
index 0000000..064f300
--- /dev/null
+++ b/rust-book/advanced/adv-fn-closure/src/main.rs
@@ -0,0 +1,79 @@
+use hello_macro::HelloMacro;
+//use hello_macro_derive::HelloMacro;
+
+struct Pancakes;
+
+impl HelloMacro for Pancakes {
+ fn hello_macro() {
+ println!("pancakes");
+ }
+}
+
+fn main() {
+ Pancakes::hello_macro();
+}
+
+//fn add_one(x: i32) -> i32 {
+// x + 1
+//}
+//
+//fn do_twice(f: fn(i32) -> i32, arg: i32) -> i32 {
+// f(arg) + f(arg)
+//}
+
+
+//fn returns_closure() -> Box<dyn Fn(i32) -> i32> {
+// Box::new(|x| x + 1)
+//}
+//
+//
+//enum Status {
+// Value(u32),
+// Stop,
+//}
+
+
+//#[macro_export]
+//macro_rules! vec {
+// ( $( $x:expr ), * ) => {
+// {
+// let mut temp_vec = Vec::new();
+// $(
+// temp_vec.push($x);
+// )*
+// temp_vec
+// }
+// };
+//}
+
+
+//#[some_attribute]
+//pub fn some_name(input: TokenStream) -> TokenStream {
+//}
+//
+//
+//fn main() {
+// let answer = do_twice(add_one, 5);
+//
+// println!("{}", answer);
+
+
+// let list_of_numbers = vec![1, 2, 3];
+// let list_of_strings: Vec<String> = list_of_numbers
+// .iter()
+// .map(ToString::to_string)
+// .collect();
+// dbg!(list_of_strings);
+
+
+// let list_of_statuses: Vec<Status> =
+// (0u32..20)
+// .map(Status::Value)
+// .collect();
+
+
+ //returns_closure();
+
+
+
+//}
diff --git a/rust-book/advanced/adv-lifetimes/Cargo.lock b/rust-book/advanced/adv-lifetimes/Cargo.lock
new file mode 100755
index 0000000..591810c
--- /dev/null
+++ b/rust-book/advanced/adv-lifetimes/Cargo.lock
@@ -0,0 +1,4 @@
+[[package]]
+name = "adv-lifetimes"
+version = "0.1.0"
+
diff --git a/rust-book/advanced/adv-lifetimes/Cargo.toml b/rust-book/advanced/adv-lifetimes/Cargo.toml
new file mode 100755
index 0000000..b127e9f
--- /dev/null
+++ b/rust-book/advanced/adv-lifetimes/Cargo.toml
@@ -0,0 +1,7 @@
+[package]
+name = "adv-lifetimes"
+version = "0.1.0"
+authors = ["Adam Carpenter <53hornet@gmail.com>"]
+edition = "2018"
+
+[dependencies]
diff --git a/rust-book/advanced/adv-lifetimes/src/main.rs b/rust-book/advanced/adv-lifetimes/src/main.rs
new file mode 100755
index 0000000..755baf4
--- /dev/null
+++ b/rust-book/advanced/adv-lifetimes/src/main.rs
@@ -0,0 +1,36 @@
+fn main() {
+ let num = 5;
+
+ let obj = Box::new(Ball { diameter: &num }) as Box<dyn Red>;
+}
+
+struct Context<'s>(&'s str);
+
+struct Parser<'c, 's: 'c> {
+ context: &'c Context<'s>,
+}
+
+impl<'c, 's> Parser<'c, 's> {
+ fn parse(&self) -> Result<(), &'s str> {
+ Err(&self.context.0[1..])
+ }
+}
+
+fn parse_context(context: Context) -> Result<(), &str> {
+ Parser { context: &context }.parse()
+}
+
+trait Red { }
+
+struct Ball<'a> {
+ diameter: &'a i32,
+}
+
+impl<'a> Red for Ball<'a> { }
+
+struct StrWrap<'a>(&'a str);
+
+fn foo(string: &str) -> StrWrap<'_> {
+ StrWrap(string)
+}
+
diff --git a/rust-book/advanced/adv-traits/Cargo.lock b/rust-book/advanced/adv-traits/Cargo.lock
new file mode 100755
index 0000000..4fb1ae5
--- /dev/null
+++ b/rust-book/advanced/adv-traits/Cargo.lock
@@ -0,0 +1,4 @@
+[[package]]
+name = "adv-traits"
+version = "0.1.0"
+
diff --git a/rust-book/advanced/adv-traits/Cargo.toml b/rust-book/advanced/adv-traits/Cargo.toml
new file mode 100755
index 0000000..b1c5f6a
--- /dev/null
+++ b/rust-book/advanced/adv-traits/Cargo.toml
@@ -0,0 +1,7 @@
+[package]
+name = "adv-traits"
+version = "0.1.0"
+authors = ["Adam Carpenter <53hornet@gmail.com>"]
+edition = "2018"
+
+[dependencies]
diff --git a/rust-book/advanced/adv-traits/src/main.rs b/rust-book/advanced/adv-traits/src/main.rs
new file mode 100755
index 0000000..659883c
--- /dev/null
+++ b/rust-book/advanced/adv-traits/src/main.rs
@@ -0,0 +1,163 @@
+//fn main() {
+// println!("Hello, world!");
+//}
+//
+//pub trait Iterator {
+// type Item;
+//
+// fn next(&mut self) -> Option<Self::Item>;
+//}
+//
+//
+//#[derive(Debug, PartialEq)]
+//struct Point {
+// x: i32,
+// y: i32,
+//}
+//
+//impl Add for Point {
+// type Output = Point;
+//
+// fn add(self, other: Point) -> Point {
+// Point {
+// x: self.x + other.x,
+// y: self.y + other.y,
+// }
+// }
+//}
+
+//fn main() {
+// assert_eq!(Point { x: 1, y: 0 } + Point { x: 2, y: 3 },
+// Point { x: 3, y: 3 });
+// println!("Done.");
+//}
+
+//trait Add<RHS=Self> {
+// type Output;
+//
+// fn add(self, rhs: RHS) -> Self::Output;
+//}
+
+//use std::ops::Add;
+//
+//struct Millimeters(u32);
+//struct Meters(u32);
+//
+//impl Add<Meters> for Millimeters {
+// type Output = Millimeters;
+//
+// fn add(self, other: Meters) -> Millimeters {
+// Millimeters(self.0 + (other.0 * 1000))
+// }
+//}
+
+
+//trait Pilot {
+// fn fly(&self);
+//}
+//
+//trait Wizard {
+// fn fly(&self);
+//}
+//
+//struct Human;
+//
+//impl Pilot for Human {
+// fn fly(&self) {
+// println!("Because I was inverted.");
+// }
+//}
+//
+//impl Wizard for Human {
+// fn fly(&self) {
+// println!("Fly, you fools.");
+// }
+//}
+//
+//impl Human {
+// fn fly(&self) {
+// println!("*feels real loose like a long-neck goose*");
+// }
+//}
+//
+//
+//trait Animal {
+// fn baby_name() -> String;
+//}
+//
+//struct Dog;
+//
+//impl Dog {
+// fn baby_name() -> String {
+// String::from("Spot")
+// }
+//}
+//
+//impl Animal for Dog {
+// fn baby_name() -> String {
+// String::from("puppy")
+// }
+//}
+
+
+//fn main() {
+// let person = Human;
+// person.fly();
+// Pilot::fly(&person);
+// Wizard::fly(&person);
+//
+//
+// println!("A baby dog is called a {}", <Dog as Animal>::baby_name());
+//
+//
+// println!("Done.");
+//}
+//
+
+
+//use std::fmt;
+//
+//trait OutlinePrint: fmt::Display {
+// fn outline_print(&self) {
+// let output = self.to_string();
+// let len = output.len();
+// println!("{}", "*".repeat(len + 4));
+// println!("*{}*", " ".repeat(len + 2));
+// println!("* {} *", output);
+// println!("*{}*", " ".repeat(len + 2));
+// println!("{}", "*".repeat(len + 4));
+// }
+//}
+//
+//struct Point {
+// x: i32,
+// y: i32,
+//}
+//
+//impl OutlinePrint for Point {}
+//
+//impl fmt::Display for Point {
+// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+// write!(f, "({}, {})", self.x, self.y)
+// }
+//}
+//
+//fn main() {
+// let point = Point { x: 5, y: 6 };
+// point.outline_print();
+//}
+
+use std::fmt;
+
+struct Wrapper(Vec<String>);
+
+impl fmt::Display for Wrapper {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ write!(f, "[{}]", self.0.join(", "))
+ }
+}
+
+fn main() {
+ let w = Wrapper(vec![String::from("hello"), String::from("world")]);
+ println!("w = {}", w);
+}
diff --git a/rust-book/advanced/adv-types/Cargo.lock b/rust-book/advanced/adv-types/Cargo.lock
new file mode 100755
index 0000000..a7ad78b
--- /dev/null
+++ b/rust-book/advanced/adv-types/Cargo.lock
@@ -0,0 +1,4 @@
+[[package]]
+name = "adv-types"
+version = "0.1.0"
+
diff --git a/rust-book/advanced/adv-types/Cargo.toml b/rust-book/advanced/adv-types/Cargo.toml
new file mode 100755
index 0000000..a3ca211
--- /dev/null
+++ b/rust-book/advanced/adv-types/Cargo.toml
@@ -0,0 +1,7 @@
+[package]
+name = "adv-types"
+version = "0.1.0"
+authors = ["Adam Carpenter <53hornet@gmail.com>"]
+edition = "2018"
+
+[dependencies]
diff --git a/rust-book/advanced/adv-types/src/main.rs b/rust-book/advanced/adv-types/src/main.rs
new file mode 100755
index 0000000..60d42b2
--- /dev/null
+++ b/rust-book/advanced/adv-types/src/main.rs
@@ -0,0 +1,41 @@
+//use std::io::Error;
+//use std::fmt;
+
+//pub trait Write {
+//
+// fn write(&mut self, buf: &[u8]) -> Result<usize>;
+// fn flush(&mut self) -> Result<(), Error>;
+//
+// fn write_all(&mut self, buf: &[u8]) -> Result<()>;
+// fn write_fmt(&mut self, fmt: fmt::Arguments) -> Result<()>;
+//}
+
+//fn main() {
+// type Miles = i32;
+//
+// let x: i32 = 5;
+// let y: Miles = 5;
+//
+// println!("x + y = {}", x + y);
+
+
+//}
+
+//type Thunk = Box<dyn Fn() + Send + 'static>;
+//
+//let f: Thunk = Box::new(|| println!("hi"));
+//
+//fn takes_long_type(f: Thunk) {
+//}
+//
+//fn returns_long_type() -> Thunk {
+//}
+
+fn main() {
+ print!("forever");
+
+ loop {
+ print!("and ever ");
+ }
+}
+
diff --git a/rust-book/advanced/hello_macro/Cargo.toml b/rust-book/advanced/hello_macro/Cargo.toml
new file mode 100755
index 0000000..e812230
--- /dev/null
+++ b/rust-book/advanced/hello_macro/Cargo.toml
@@ -0,0 +1,7 @@
+[package]
+name = "hello_macro"
+version = "0.1.0"
+authors = ["Adam Carpenter <53hornet@gmail.com>"]
+edition = "2018"
+
+[dependencies]
diff --git a/rust-book/advanced/hello_macro/src/lib.rs b/rust-book/advanced/hello_macro/src/lib.rs
new file mode 100755
index 0000000..e747931
--- /dev/null
+++ b/rust-book/advanced/hello_macro/src/lib.rs
@@ -0,0 +1,3 @@
+pub trait HelloMacro {
+ fn hello_macro();
+}
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
+}
+