اینجا بیشتر میتونید در موردش بخونید.
https://doc.rust-lang.org/book/ch06-00-enums.html
مثلا استفاده کردن match در کنار Result ENUM خیلی practice خوبی برای بررسی خروجی یک تابع یا متد هست.
اینطوری مجبور نیستی همه توابع رو طوری بنویسی که خروجی آخرشون error باشه (در گولنگ اینطوریه)
به کمک pattern matching هم خیلی خوانایی کد کمتر نمیشه بنظرم.
let greeting_file_result = File::open("hello.txt");
let greeting_file = match greeting_file_result {
Ok(file) => file,
Err(error) => match error.kind() {
ErrorKind::NotFound => match File::create("hello.txt") {
Ok(fc) => fc,
Err(e) => panic!("Problem creating the file: {e:?}"),
},
other_error => {
panic!("Problem opening the file: {other_error:?}");
}
},
};
از نظر error handling فکر میکنم نگاه مشترکی با گولنگ داره، از این نظر که خطاهارو دو دسته recoverable و unrecoverable میبینه که یکی رو با Result مدیریت میکنه و یکی رو با panic که فکر میکنم خیلی شبیه نگاه گولنگ هست.
Most languages don’t distinguish between these two kinds of errors and handle both in the same way, using mechanisms such as exceptions. Rust doesn’t have exceptions. Instead, it has the type Result<T, E> for recoverable errors and the panic! macro that stops execution when the program encounters an unrecoverable error.
@gocasts
#rust #golang