Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::fmt::Display;
- use std::fmt::Formatter;
- use std::fmt::Error;
- use std::ops::Deref;
- use std::ops::DerefMut;
- struct MyResult<T, E>(Result<T, E>);
- impl<T: Display, E: Display> Display for MyResult<T, E> {
- fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
- match self.0 {
- Ok(ref v) => write!(f, "Ok({})", v),
- Err(ref v) => write!(f, "Err({})", v),
- }
- }
- }
- ///////////////// This is not mandatory
- impl<T, U> Deref for MyResult<T, U> {
- type Target = Result<T, U>;
- fn deref(&self) -> &Result<T, U> {
- &self.0
- }
- }
- impl<T, U> DerefMut for MyResult<T, U> {
- fn deref_mut(&mut self) -> &mut Result<T, U> {
- &mut self.0
- }
- }
- //////////////////
- fn main() {
- let result: Result<u32, String> = Result::Ok(50);
- println!("{:?}", result);
- let my_result = MyResult(result);
- println!("{}", my_result);
- // If you implement Deref, you can keep using Result methods
- if my_result.is_ok() {
- println!("All good");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement