From 68d838f8b6ad1d02e8255be56bf8f4744d2fbaf0 Mon Sep 17 00:00:00 2001 From: Lukas Kalbertodt Date: Mon, 9 Jan 2017 18:43:34 +0100 Subject: [PATCH] Add solution sheet9 --- aufgaben/sheet9/sol1/try-opt.rs | 36 ++++++++++++++++++++++++++++ aufgaben/sheet9/sol2/fmt.rs | 42 +++++++++++++++++++++++++++++++++ aufgaben/sheet9/sol3/map.rs | 15 ++++++++++++ 3 files changed, 93 insertions(+) create mode 100755 aufgaben/sheet9/sol1/try-opt.rs create mode 100755 aufgaben/sheet9/sol2/fmt.rs create mode 100755 aufgaben/sheet9/sol3/map.rs diff --git a/aufgaben/sheet9/sol1/try-opt.rs b/aufgaben/sheet9/sol1/try-opt.rs new file mode 100755 index 0000000..d30015b --- /dev/null +++ b/aufgaben/sheet9/sol1/try-opt.rs @@ -0,0 +1,36 @@ +// If we would make this public for everyone, we would need to use absolute +// paths! +macro_rules! try_opt { + ($e:expr) => { + match $e { + ::std::option::Option::Some(v) => v, + None => return None, + } + } +} + +macro_rules! name { + () => ($crate) +} + +fn main() { + println!("{:?}", name!()); + println!("consistent: {:?}", is_home_consistent()); + println!("dummy: {:?}", foo()); +} + +fn foo() -> Option { + let a: u8 = try_opt!(Some(50)); + let b = try_opt!(a.checked_mul(6)); + + Some(b / 2) +} + +fn is_home_consistent() -> Option { + use std::env; + + let home_dir = try_opt!(env::home_dir()); + let home_var = try_opt!(env::var_os("HOME")); + + Some(home_dir == home_var) +} diff --git a/aufgaben/sheet9/sol2/fmt.rs b/aufgaben/sheet9/sol2/fmt.rs new file mode 100755 index 0000000..4cf5548 --- /dev/null +++ b/aufgaben/sheet9/sol2/fmt.rs @@ -0,0 +1,42 @@ +struct LoveMachine { + inner: T, +} + +impl LoveMachine { + pub fn new(t: T) -> Self { + LoveMachine { + inner: t, + } + } +} + +macro_rules! impl_fmt { + ($fmt_trait:ident, $fmt_str:expr) => { + impl ::std::fmt::$fmt_trait for LoveMachine + where T: ::std::fmt::$fmt_trait + { + fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { + write!(f, concat!("♥", $fmt_str, "♥"), self.inner) + } + } + } +} + +impl_fmt!(Display, "{}"); +impl_fmt!(Debug, "{:?}"); +impl_fmt!(Octal, "{:o}"); +impl_fmt!(LowerHex, "{:x}"); +impl_fmt!(UpperHex, "{:X}"); +impl_fmt!(Binary, "{:b}"); +impl_fmt!(LowerExp, "{:e}"); +impl_fmt!(UpperExp, "{:E}"); + +fn main() { + let lm = LoveMachine::new(27); + println!("Display: {}", lm); + println!("Debug: {:?}", lm); + println!("Octal: {:o}", lm); + println!("LowerHex: {:x}", lm); + println!("UpperHex: {:X}", lm); + println!("Binary: {:b}", lm); +} diff --git a/aufgaben/sheet9/sol3/map.rs b/aufgaben/sheet9/sol3/map.rs new file mode 100755 index 0000000..142471b --- /dev/null +++ b/aufgaben/sheet9/sol3/map.rs @@ -0,0 +1,15 @@ + +macro_rules! hash_map { + ( $($key:expr => $value:expr ,)* ) => {{ + let mut map = ::std::collections::HashMap::new(); + $( map.insert($key, $value); )* + map + }}; + ( $($key:expr => $value:expr),* ) => { hash_map!($($key => $value ,)*) }; +} + + +fn main() { + let ages = hash_map!{ "Sabine" => 26, "Peter" => 32 }; + println!("{:#?}", ages); +}