Last active
September 6, 2024 12:15
-
-
Save Eveeifyeve/ddc992ee08da8d82477d7cfd62df7db1 to your computer and use it in GitHub Desktop.
Rust impl_trait Optional
This file contains 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
| |
trait Optional<T>: Into<Option<T>> {} | |
impl<T> Optional<T> for T where T: Into<Option<T>> {} | |
impl<T> Optional<T> for Option<T> {} | |
fn test<T: Optional<bool>>(test_type: T) -> bool { | |
let test_type = match test_type.into() { | |
Some(test) => test, | |
None => false, | |
}; | |
test_type | |
} | |
pub fn main() { | |
println!("{}", test(Some(true))); | |
println!("{}", test(None)); | |
println!("{}", test(false)); | |
println!("{}", test(true)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment