Rust – il linguaggio – 50

FoLug

Proseguo, oggi qui: /usr/local/share/doc/rust/html/book/error-handling.html.

Gestione degli errori

Panico (mio, non panic!) perché: This chapter is very long, mostly because we start at the very beginning with sum types and combinators, and try to motivate the way Rust does error handling incrementally. As such, programmers with experience in other expressive type systems may want to jump around.

Segue un lungo indice molto dettagliato (la documentazione di Rust è davvero ottima) che non copio; c’è tutto là 😀
C’è anche, per finire, un esempio pratico (case study), molto interessante, da fare se si decide che Rust è da usarsi.

E alla fine il riassunto del capitolo 😀

Riassunto

Since this chapter is long, it is useful to have a quick summary for error handling in Rust. These are some good “rules of thumb.” They are emphatically not commandments. There are probably good reasons to break every one of these heuristics!

  • If you’re writing short example code that would be overburdened by error handling, it’s probably just fine to use unwrap (whether that’s Result::unwrap [/usr/local/share/doc/rust/html/std/result/enum.Result.html#method.unwrap], Option::unwrap [/usr/local/share/doc/rust/html/std/option/enum.Option.html#method.unwrap] or preferably Option::expect [/usr/local/share/doc/rust/html/std/option/enum.Option.html#method.expect]). Consumers of your code should know to use proper error handling. (If they don’t, send them here!)
  • If you’re writing a quick ‘n’ dirty program, don’t feel ashamed if you use unwrap. Be warned: if it winds up in someone else’s hands, don’t be surprised if they are agitated by poor error messages!
  • If you’re writing a quick ‘n’ dirty program and feel ashamed about panicking anyway, then use either a String or a Box<Error + Send + Sync> for your error type (the Box<Error + Send + Sync> type is because of the available From impls [/usr/local/share/doc/rust/html/std/convert/trait.From.html]).
  • Otherwise, in a program, define your own error types with appropriate From and Error [/usr/local/share/doc/rust/html/std/error/trait.Error.html] impls to make the try! macro more ergonomic.
  • If you’re writing a library and your code can produce errors, define your own error type and implement the std::error::Error trait. Where appropriate, implement From to make both your library code and the caller’s code easier to write. (Because of Rust’s coherence rules, callers will not be able to impl From on your error type, so your library should do it.)
  • Learn the combinators defined on Option [/usr/local/share/doc/rust/html/std/option/enum.Option.html] and Result [/usr/local/share/doc/rust/html/std/result/enum.Result.html]. Using them exclusively can be a bit tiring at times, but I’ve personally found a healthy mix of try! and combinators to be quite appealing. and_then, map and unwrap_or are my favorites.

Rust è OK ma chissà se lo userò 😳
:mrgreen:

Posta un commento o usa questo indirizzo per il trackback.

Trackback

Rispondi

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo di WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione /  Modifica )

Google photo

Stai commentando usando il tuo account Google. Chiudi sessione /  Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione /  Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione /  Modifica )

Connessione a %s...

Questo sito utilizza Akismet per ridurre lo spam. Scopri come vengono elaborati i dati derivati dai commenti.

%d blogger hanno fatto clic su Mi Piace per questo: