summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Carpenter <53hornet@gmail.com>2019-03-22 16:48:03 -0400
committerAdam Carpenter <53hornet@gmail.com>2019-03-22 16:48:03 -0400
commitcbfe2f1de7f0e24525e641228fec4c659badde93 (patch)
tree5939189ff3e3178e4f740a6038c6ccea7a182c0d
parentb1ea85616e4367713afbeabb6cfb6ae553c17a60 (diff)
downloadlearning-rust-cbfe2f1de7f0e24525e641228fec4c659badde93.tar.xz
learning-rust-cbfe2f1de7f0e24525e641228fec4c659badde93.zip
Added advanced traits, types, functions.
-rw-r--r--advanced/adv-fn-closure/Cargo.toml7
-rw-r--r--advanced/adv-fn-closure/src/main.rs13
-rw-r--r--advanced/adv-traits/src/main.rs161
-rw-r--r--advanced/adv-types/Cargo.toml7
-rw-r--r--advanced/adv-types/src/main.rs41
5 files changed, 213 insertions, 16 deletions
diff --git a/advanced/adv-fn-closure/Cargo.toml b/advanced/adv-fn-closure/Cargo.toml
new file mode 100644
index 0000000..259562e
--- /dev/null
+++ b/advanced/adv-fn-closure/Cargo.toml
@@ -0,0 +1,7 @@
+[package]
+name = "adv-fn-closure"
+version = "0.1.0"
+authors = ["Adam Carpenter <53hornet@gmail.com>"]
+edition = "2018"
+
+[dependencies]
diff --git a/advanced/adv-fn-closure/src/main.rs b/advanced/adv-fn-closure/src/main.rs
new file mode 100644
index 0000000..d31bd90
--- /dev/null
+++ b/advanced/adv-fn-closure/src/main.rs
@@ -0,0 +1,13 @@
+fn add_one(x: i32) -> i32 {
+ x + 1
+}
+
+fn do_twice(f: fn(i32) -> i32, arg: i32) -> i32 {
+ f(arg) + f(arg)
+}
+
+fn main() {
+ let answer = do_twice(add_one, 5);
+
+ println!("{}", answer);
+}
diff --git a/advanced/adv-traits/src/main.rs b/advanced/adv-traits/src/main.rs
index 6ec0aac..659883c 100644
--- a/advanced/adv-traits/src/main.rs
+++ b/advanced/adv-traits/src/main.rs
@@ -8,27 +8,156 @@
// fn next(&mut self) -> Option<Self::Item>;
//}
//
-use std::ops::Add;
+//
+//#[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,
+// }
+// }
+//}
-#[derive(Debug, PartialEq)]
-struct Point {
- x: i32,
- y: i32,
-}
+//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;
-impl Add for Point {
- type Output = Point;
+struct Wrapper(Vec<String>);
- fn add(self, other: Point) -> Point {
- Point {
- x: self.x + other.x,
- y: self.y + other.y,
- }
+impl fmt::Display for Wrapper {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ write!(f, "[{}]", self.0.join(", "))
}
}
fn main() {
- assert_eq!(Point { x: 1, y: 0 } + Point { x: 2, y: 3 },
- Point { x: 3, y: 3 });
- println!("Done.");
+ let w = Wrapper(vec![String::from("hello"), String::from("world")]);
+ println!("w = {}", w);
}
diff --git a/advanced/adv-types/Cargo.toml b/advanced/adv-types/Cargo.toml
new file mode 100644
index 0000000..a3ca211
--- /dev/null
+++ b/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/advanced/adv-types/src/main.rs b/advanced/adv-types/src/main.rs
new file mode 100644
index 0000000..60d42b2
--- /dev/null
+++ b/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 ");
+ }
+}
+