Skip to content
0:00 / 0:00

#rust#errorhandling#anyhow

Rust Error Handling - Best Practices

Jeremy Chone

33K subscribers • 3 videos on Hallway

YouTube
  • Recommended by 2 people: 2 in engineering communities.
  • Presented by Jeremy Chone, software engineer
  • Linked in the descriptionrust10x/rust-web-app

Intermediate. Assumes working knowledge.

49K views2 years ago2 recommendations

Programming languagesSoftware designRustDesign patterns

Jeremy Chone explains effective patterns and best practices for handling errors in Rust, from simple tests to production-grade applications.

Here are some best practices for error handling in Rust programming that I've found effective: - For tests and examples, use "box dyn error" instead of "anyhow." - Prefer the "?" pattern over "unwrap" or "expect," even in tests and examples, to align with production coding standards. - "Box dyn error" is excellent for error passthrough, as are `map_err` and `ok_or` with static strings and formatted strings. - In production code, use an enum with fully descriptive variant names and favor struct variants with clear names over tuple variants. - Avoid relying on the display to convey the meaning of error variants. - In web or device applications, human error rendering might not occur at the enum/type level, so using Debug for display may suffice. - For web or device applications, using Serialize for errors that need to be logged or sent to the client for display can be a powerful and straightforward pattern. - Prioritize clarity in error naming (variants) and focus on consistency as the code develops. - Don't be afraid of medium-length enum variant names; remember, type refactoring is straightforward. - In summary, start with progressive and clear practices, and become more structured as the code matures. == Jeremy Chone: - Patreon - patreon.com/jeremychone - Any help is a big help (for Rust educational content) - Twitter - twitter.com/jeremychone - Discord general-rust - discord.gg/W2besKCzjx - Discord rust10x - discord.gg/XuKWrNGKpC - Rust10x - rust10x.com - Rust resources for production coding. Big thanks to CrabNebula (crabnebula.dev/) for sponsoring this channel. == Rust10x AI / OpenAI / Ollama - Rust OpenAI API Tutorial: youtube.com/watch?v=PHbCmIckV20&list=PL7r-PXl6ZPcCIOFaL7nVHX… - Rust Ollama Tutorial: youtu.be/OcH-zT5VNgM == Rust10x Web App production coding: Episode 01: youtube.com/watch?v=3cA_mk4vdWY&list=PL7r-PXl6ZPcCIOFaL7nVHX… Episode 02: youtube.com/watch?v=-dMH9UiwKqg&list=PL7r-PXl6ZPcCIOFaL7nVHX… Episode 03: youtube.com/watch?v=zUxF0kvydJs&list=PL7r-PXl6ZPcCIOFaL7nVHX… Episode 04: youtube.com/watch?v=3E0zK5h9zEs&list=PL7r-PXl6ZPcCIOFaL7nVHX… Episode 05.1: youtube.com/watch?v=Gc5Nj5LJe1U&list=PL7r-PXl6ZPcCIOFaL7nVHX… Web Site: rust10x.com/web-app GitHub: github.com/rust10x/rust-web-app Discord rust10x - discord.gg/XuKWrNGKpC Patreon - patreon.com/jeremychone - Any help is a big help (for Rust Production Coding educational content) == Other Other popular Rust Programming videos: - Rust Web App Production Coding Blueprint - youtube.com/watch?v=3cA_mk4vdWY&list=PL7r-PXl6ZPcCIOFaL7nVHX… - Rust Axum - Full Course - youtube.com/watch?v=XZtlD_m59sM&list=PL7r-PXl6ZPcCIOFaL7nVHX… - Rust Type State Builder Pattern - youtube.com/watch?v=pwmIQzLuYl0&list=

Why it's here

2 people recommend this

What people say. Recommended as a helpful guide for learning clean and idiomatic error handling patterns in Rust.

u/meowsqueakin r/rust • Recommends

On “What are some Rust practices you learned in a real workplace? ”

I've spent a few months writing real Rust production code, so I can perhaps share my experience. ---- I have found Rust's type system very effective at constraining data to valid values, and simplifying the downstream code. A lot of my work involves parsing and data conversion, and I find the `struct` + `enum` building blocks to be very powerful for modelling the input and output data. Then conversion often becomes a large set of `TryFrom` or `From` trait implementations. For anything that can only be validated at run-time, I use private struct members and constructors. This significantly reduces the test space, as well as allowing me to leverage validity assumptions further down the data…

u/BarneyBuffetin r/rust • Recommends

On “Better way to unwrap errors”

Check out this youtube video: [ Rust Error Handling - Best Practice]