Last active
August 29, 2015 14:11
-
-
Save daboross/94518b2e221ae14b9c48 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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