코딩Coding/한글Rust강의★내가★공부하려고만듬

Rust Error❤️Handling 외우자❤️공식 & anyhow_easyrust만세

내인생PLUS 2022. 5. 23. 12:54
728x90

 

use std::error::Error;
use std::fmt::{Formatter, Display};

#[derive(Debug)]
enum CompanyError {
    CouldntConnect,
    NotEnoughData,
    UserTimedOut
}

impl Display for CompanyError {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error>{
        write!(f, "Got a CompanyError")
    }
}

#[derive(Debug)]
struct BaseError;


impl Display for BaseError {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error>{
        write!(f, "Got a BaseError")
    }
}


impl Error for CompanyError {
    
}


impl Error for BaseError {
    
}


// use std::num::ParseIntError;


// fn try_to_make_number(int_input: &str, float_input: &str) -> Result<(), ParseIntError> {
//     let my_number = int_input.parse::<i32>()?;
//     let other_number = float_input.parse<f32>()?;
//     Ok(())
// }

fn main () {
    
}


출처
139 error
https://youtu.be/hrPO-J_texs

 

use std::error::Error;
use std::fmt::{Formatter, Display};

#[derive(Debug)]
enum CompanyError {
    CouldntConnect,
    NotEnoughData,
    UserTimedOut
}

impl Display for CompanyError {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error>{
        write!(f, "Got a CompanyError")
    }
}

#[derive(Debug)]
struct BaseError;

fn check_thing(is_okay: bool) -> Result<(), Box<dyn Error>> {
    if is_okay {
        Err(Box::new(CompanyError::CouldntConnect))
    } else {
        Err(Box::new(BaseError))
    }
}




impl Display for BaseError {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error>{
        write!(f, "Got a BaseError")
    }
}


impl Error for CompanyError {
    
}


impl Error for BaseError {
    
}


// use std::num::ParseIntError;


// fn try_to_make_number(int_input: &str, float_input: &str) -> Result<(), ParseIntError> {
//     let my_number = int_input.parse::<i32>()?;
//     let other_number = float_input.parse<f32>()?;
//     Ok(())
// }

fn main () {
    
}


error2

use std::error::Error;
use std::fmt::{Formatter, Display};

#[derive(Debug)]
enum CompanyError {
    CouldntConnect,
    NotEnoughData,
    UserTimedOut
}

impl Display for CompanyError {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error>{
        write!(f, "Got a CompanyError")
    }
}

#[derive(Debug)]
struct BaseError;

fn give_error(is_company_error: bool) -> Box<dyn Error> {
    if is_company_error {
        Box::new(CompanyError::CouldntConnect)
    } else {
        Box::new(BaseError)
    }
}




impl Display for BaseError {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error>{
        write!(f, "Got a BaseError")
    }
}


impl Error for CompanyError {
    
}


impl Error for BaseError {
    
}


// use std::num::ParseIntError;


// fn try_to_make_number(int_input: &str, float_input: &str) -> Result<(), ParseIntError> {
//     let my_number = int_input.parse::<i32>()?;
//     let other_number = float_input.parse<f32>()?;
//     Ok(())
// }

fn main () {
    let error_1 = give_error(true);
    let error_2 = give_error(false);

    println!("{error_1}, {error_2}");
    
}



error2
https://youtu.be/GVm-OinlnAA

error공식 문서

https://doc.rust-lang.org/std/error/trait.Error.html

 

Error in std::error - Rust

Error is a trait representing the basic expectations for error values, i.e., values of type E in Result . Errors must describe themselves through the Display and Debug traits. Error messages are typically concise lowercase sentences without trailing punctu

doc.rust-lang.org


Anyhow








반응형