Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.