8 Common Mistakes New Rust Developers Make

TLDRIn this video, we cover 8 common mistakes new Rust developers make and a bonus mistake that even experienced developers make. We discuss unnecessary indirection, overusing slice indexing, using sentinel values, not taking advantage of enums and pattern matching, error handling, not utilizing standard library traits and macros, and not using Rust's tooling effectively.

Key insights

🔑Unnecessary indirection can be avoided by using string slices instead of owning strings.

🔑Rust provides alternatives to slice indexing, such as the array Windows method and using map instead of for loops.

🔑Using sentinel values can be replaced with Rust's Option enum, which represents optional values.

🔑Taking advantage of enums and pattern matching can lead to more robust and readable code.

🔑Proper error handling involves using the question mark operator and implementing the error trait.

Q&A

What is the benefit of using string slices instead of owning strings?

Using string slices reduces unnecessary indirection and provides more flexibility in the API.

Why should developers avoid slice indexing?

Slice indexing can lead to index out of bounds errors, but Rust provides alternatives like array Windows method and map function.

What are sentinel values and why should they be avoided?

Sentinel values are special values with specific meanings, but Rust's Option enum provides a safer and more explicit way to represent optional values.

Why is pattern matching important?

Pattern matching allows for more robust code by matching specific patterns and handling each case appropriately.

How should errors be handled in Rust?

Errors should be handled using the question mark operator for error propagation and implementing the error trait for custom error types.

Timestamped Summary

00:00Introduction

00:11Unnecessary indirection

01:56Overusing slice indexing

02:57Using sentinel values

04:53Not taking advantage of enums and pattern matching

06:49Error handling

07:50Not utilizing standard library traits and macros

08:58Not using Rust's tooling effectively

09:59Bonus mistake: Overusing RC and RefCell