-
Rust Error❤️Handling 외우자❤️공식 & anyhow_easyrust만세코딩Coding/한글Rust강의★내가★공부하려고만듬 2022. 5. 23. 12:54728x90
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_texsuse 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 () { }
error2use 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-OinlnAAerror공식 문서
https://doc.rust-lang.org/std/error/trait.Error.html
Anyhow
반응형'코딩Coding > 한글Rust강의★내가★공부하려고만듬' 카테고리의 다른 글
Rust Bevy 0.7❤️- Full Tutorial - Game Development (0) 2022.05.26 A Singly Linked List❤️ in Rust - Ryan Levick (0) 2022.05.25 Rust Node❤️연습 예정 데이터 관리-LRU Cache- Rust Programming Exercises (0) 2022.05.25 러스트에도 파이썬 넘파이와 비슷한 라이브러리가 있다?rust vs python numpy #rust #ndarray #numpy (0) 2022.05.23 한글Rust러스트❤️015_Option_part3_Implementing_Vec_Rustonomicon (0) 2022.05.22 Rust❤️)enum, struct등등 용량 계산하기 (0) 2022.05.22 C++ vs Rust Reference & Dereference 코딩 비교❤️ (0) 2022.05.20 VSCode활용법--diff❤️2개 코드 비교해서 보기_틀린 곳 찾기 #vscode #diff (0) 2022.05.19