mirror of
https://github.com/LukasKalbertodt/programmieren-in-rust.git
synced 2025-06-29 08:17:32 +02:00
Add solution sheet9
This commit is contained in:
42
aufgaben/sheet9/sol2/fmt.rs
Executable file
42
aufgaben/sheet9/sol2/fmt.rs
Executable file
@ -0,0 +1,42 @@
|
||||
struct LoveMachine<T> {
|
||||
inner: T,
|
||||
}
|
||||
|
||||
impl<T> LoveMachine<T> {
|
||||
pub fn new(t: T) -> Self {
|
||||
LoveMachine {
|
||||
inner: t,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! impl_fmt {
|
||||
($fmt_trait:ident, $fmt_str:expr) => {
|
||||
impl<T> ::std::fmt::$fmt_trait for LoveMachine<T>
|
||||
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);
|
||||
}
|
Reference in New Issue
Block a user