Created
December 3, 2019 21:38
-
-
Save o0Ignition0o/d6d0744ecec647ecb99642c101542e95 to your computer and use it in GitHub Desktop.
on joue avec des results
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
use rand::prelude::*; | |
#[derive(Debug)] | |
struct JaiPasDentierError {} | |
#[derive(Debug)] | |
struct JaiPasReussiError {} | |
#[derive(Debug)] | |
enum MyError { | |
PasDentier(JaiPasDentierError), | |
PasReussi(JaiPasReussiError), | |
} | |
impl From<JaiPasDentierError> for MyError { | |
fn from(e: JaiPasDentierError) -> Self { | |
Self::PasDentier(e) | |
} | |
} | |
impl From<JaiPasReussiError> for MyError { | |
fn from(e: JaiPasReussiError) -> Self { | |
Self::PasReussi(e) | |
} | |
} | |
fn donne_moi_un_entier_stp() -> Result<i32, JaiPasDentierError> { | |
if rand::random() { | |
Ok(1234) | |
} else { | |
Err(JaiPasDentierError {}) | |
} | |
} | |
fn multiplie_le_par_deux(i: i32) -> i32 { | |
i * 2 | |
} | |
fn tente_un_truc_avec(i: i32) -> Result<i32, JaiPasReussiError> { | |
if rand::random() { | |
Ok(i * 3) | |
} else { | |
Err(JaiPasReussiError {}) | |
} | |
} | |
fn main() -> Result<(), MyError> { | |
let entier = donne_moi_un_entier_stp()?; | |
let fois_deux = multiplie_le_par_deux(entier); | |
let reussi = tente_un_truc_avec(fois_deux)?; | |
println!("{}", reussi); | |
Ok(()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment