Skip to content

Instantly share code, notes, and snippets.

@o0Ignition0o
Created December 3, 2019 21:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save o0Ignition0o/d6d0744ecec647ecb99642c101542e95 to your computer and use it in GitHub Desktop.
Save o0Ignition0o/d6d0744ecec647ecb99642c101542e95 to your computer and use it in GitHub Desktop.
on joue avec des results
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