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 UnwrapOrEmpty<'a> { | |
fn unwrap_or_empty(&self) -> &'a String; | |
} | |
impl<'a> UnwrapOrEmpty<'a> for Option<&'a String> { | |
fn unwrap_or_empty(&self) -> &'a String { | |
self.unwrap_or_else(||{ // use a closure | |
static DEFAULT: String = String::new(); // declare a static String | |
&DEFAULT // return a reference to a String to match HashMap::get() | |
}) | |
} |
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 A { | |
fn as_any_mut(&mut self) -> &mut dyn std::any::Any; | |
} | |
struct B<T> { | |
_unused: T | |
} | |
impl<T: 'static> A for B<T> { // static, else: "the parameter type `T` may not live long enough" | |
fn as_any_mut(&mut self) -> &mut dyn std::any::Any { | |
self | |
} |