Skip to content

Instantly share code, notes, and snippets.

@daboross
Last active August 29, 2015 14:11
Show Gist options
  • Save daboross/94518b2e221ae14b9c48 to your computer and use it in GitHub Desktop.
Save daboross/94518b2e221ae14b9c48 to your computer and use it in GitHub Desktop.
pub trait Logger {
fn log(&self, msg: &str);
}
pub struct LoggerImpl1;
pub struct LoggerImpl2;
impl LoggerImpl1 {
pub fn new() -> LoggerImpl1 {
return LoggerImpl1;
}
}
impl LoggerImpl2 {
pub fn new() -> LoggerImpl2 {
return LoggerImpl2;
}
}
impl Logger for LoggerImpl1 {
fn log(&self, msg: &str) {
}
}
impl Logger for LoggerImpl2 {
fn log(&self, msg: &str) {
}
}
extern crate logging;
use std::rand;
fn main() {
let logger: Box<logging::Logger> = match rand::random::<bool>() {
true => box logging::LoggerImpl1::new(),
false => box logging::LoggerImpl2::new(),
};
logger.log("text");
}
Compiling logging v0.0.1 (file:///home/daboross/Projects/Rust/logging)
/home/daboross/Projects/Rust/logging/src/main.rs:6:40: 9:6 error: match arms have incompatible types: expected `Box<logging::LoggerImpl1>`, found `Box<logging::LoggerImpl2>` (expected struct logging::LoggerImpl1, found struct logging::LoggerImpl2)
/home/daboross/Projects/Rust/logging/src/main.rs:6 let logger: Box<logging::Logger> = match rand::random::<bool>() {
/home/daboross/Projects/Rust/logging/src/main.rs:7 true => box logging::LoggerImpl1::new(),
/home/daboross/Projects/Rust/logging/src/main.rs:8 false => box logging::LoggerImpl2::new(),
/home/daboross/Projects/Rust/logging/src/main.rs:9 };
/home/daboross/Projects/Rust/logging/src/main.rs:8:18: 8:49 note: match arm with an incompatible type
/home/daboross/Projects/Rust/logging/src/main.rs:8 false => box logging::LoggerImpl2::new(),
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error: aborting due to previous error
Could not compile `logging`.
To learn more, run the command again with --verbose.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment