Pattern matching within Rust functions for type-safe APIs - Learn Rust Creatively

Опубликовано: 09 Октябрь 2024
на канале: timClicks
1,479
57

Let's explain a hidden feature of Rust's function signatures - the ability to specify arbitrary patterns where you would normally see a parameter's name. this is used in the Axum web framework, and arguably it should be more common.

The second half of this video gets fairly dense quite quickly.

Highlights

Explaining path(Path(user_id): Path[u32]) - 04:00 (replace [] with angle brackets)
What str means: 16:55
What ?Sized means: 18:35

Chapters

00:00:00 Introduction
00:01:31 Expaining the problem - Axum's confusing syntax with extractors
00:03:40 Path extractor example
00:05:10 General purpose pattern matching in functions
00:06:00 Rust Reference: Functions
00:07:23 Motivating the syntax - destructuring Price(f32)
00:11:06 Destructuring a struct with named fields
00:15:10 Introducing the Deref trait
00:15:44 Call methods on str from String and str thanks to Deref
00:18:11 Implementing Deref
00:22:08 Mixing Deref and std::ops:Add
00:27:23 Summary and wrap up


📚 Resources:
Rust reference on patterns: https://doc.rust-lang.org/reference/p...
Axum


🦀 Rust resources:
Tim's tutorial videos https://timclicks.dev
Rust Documentation: https://doc.rust-lang.org/book/
Rust Playground: https://play.rust-lang.org/
Rust in Action (Tim's book!) https://mng.bz/4MlD
How to Learn Rust (online course!) https://learning.accelerant.dev/how-t...

👋 Connect with Tim:
Twitter:   / timclicks  
GitHub: https://github.com/timClicks
Mastodon: https://mastodon.nz/@timClicks
DEV: https://dev.to/timclicks/
Patreon (extra learning materials)   / timclicks  


🔔 Subscribe to the channel and click the bell icon to stay updated with the latest videos and live streams from timClicks: https://www.youtube.com/timClicks?sub...

👍 Like this video if you found it helpful, and share it with your friends who are also interested in Rust programming.