Generic Traits, Impls, and Slices in Rustlang

  Рет қаралды 11,822

chris biscardi

chris biscardi

Күн бұрын

Пікірлер: 42
@phenanrithe
@phenanrithe Жыл бұрын
Note that it's possible to use an associated type instead of a generic trait, which has the benefit of being usable as reference, for instance in binds (besides, there is no alternative for T so the generic is redundant and only gives more work to the compiler). The changes are minor in the code (unfortunately it's not possible to post URLs here), just remove the after Prefix, add "type Data;" in the trait definition and "Type Data = T;" in the blanket implementation, then replace the remaining "T"s with "Self::Data".
@peter9477
@peter9477 Жыл бұрын
I was very briefly confused by the name "prefix" when it's more of a subsequence, but other than that it was clear.
@Taernsietr
@Taernsietr Жыл бұрын
yep, especially coming from a linguistics background, it threw me off at the start, but the explanation was otherwise so clear that it didn't matter hahah
@chrisbiscardi
@chrisbiscardi Жыл бұрын
Thanks for sharing that. Definitely could've chosen a better name there.
@rotteegher39
@rotteegher39 Жыл бұрын
@@chrisbiscardi inside_of() ? has() ? inside()?
@nceevij
@nceevij Жыл бұрын
First channel with such a great deal. After seeing this I felt I don't know anything, I have so much to learn in Rust. Long way to go
@FourLowAdventures
@FourLowAdventures 8 ай бұрын
Been 8 months since you wrote this, how is it coming along?
@rotteegher39
@rotteegher39 Жыл бұрын
I felt like big brain rusty kettle after watching this detailed explanation. Thank you!
@chris.davidoff
@chris.davidoff Жыл бұрын
I didn't expect this to make sense but it did after watching it!! thank you!
@10produz90
@10produz90 Жыл бұрын
Awesome explanation. Love that you go over everything in detail
@Jordans1882
@Jordans1882 Жыл бұрын
Thanks. This was a really well done video. Your in depth walk-through of the code and types helped a lot. I also thought your example was perfect to get the point across for understanding generics and traits.
@ZekeFast
@ZekeFast Жыл бұрын
Good intro! There really mind blowing things you can done with generics, like instead of defining implementations directly for &[T] from your example we can define one for the types for which Deref coercion to &[T] exists. It is one of the most powerful things in type system and a great way to DRY code as alternative to using macros.
@goodwish1543
@goodwish1543 Жыл бұрын
Beautiful! Thanks for sharing. Looking forward to more intermediate Rust contents like this. :)
@tedrose
@tedrose Жыл бұрын
Great content, my favorite Rust youtuber right now!
@sunitjoshi3573
@sunitjoshi3573 11 ай бұрын
Great walkthrough especially for newbies, like me.
@OlivierGeorg
@OlivierGeorg 6 ай бұрын
At 10:00, why is it the same comparing values or references? Can't references be different but values the same?
@chrisbiscardi
@chrisbiscardi 6 ай бұрын
Rust shared reference PartialEq compares the value the reference points to, not the pointer itself. doc.rust-lang.org/src/core/cmp.rs.html#1674-1676 when working with raw pointers you'd use ptr::eq or similar instead: doc.rust-lang.org/std/ptr/fn.eq.html
@irlshrek
@irlshrek Жыл бұрын
i love this channel
@m-7172
@m-7172 Жыл бұрын
It seems to me, that if we were looking for a subsequence [3, 4, 5] in [1, 2, 3, 4] your program would panic, because (index + prefix.len()) might be larger than the index of the last element in the original vec.
@m-7172
@m-7172 Жыл бұрын
This should fix it: fn has_prefix(&self, prefix: &[T]) -> bool { self.iter() .positions(|v| *v == prefix[0]) .find(|&index| { if (self.len() - index) >= prefix.len() { let range = index..(index + prefix.len()); self[range] == *prefix } else { false } }) .is_some() }
@chrisbiscardi
@chrisbiscardi Жыл бұрын
Congrats, you were the first person to complete the challenge in the description! notably prefix[0] could also panic if an empty slice was passed in
@m-7172
@m-7172 Жыл бұрын
@@chrisbiscardi ngl I missed both the challenge in the description and the possibility of passing an empty slice. Nevertheless this one should do it: fn has_prefix(&self, prefix: &[T]) -> bool { self.iter() .positions(|v| if prefix.is_empty(){ false } else { *v == prefix[0] } ) .find(|&index| { if (self.len() - index) >= prefix.len() { let range = index..(index + prefix.len()); self[range] == *prefix } else { false } }) .is_some() }
@echobucket
@echobucket Жыл бұрын
Is there a reason you can't just use windows() in your trait implementation?
@chrisbiscardi
@chrisbiscardi Жыл бұрын
I wanted to show slices and equality is all
@ewe-studios
@ewe-studios 11 ай бұрын
What extension do you use to hide and show the type in vscode or is it part of vscode rust extension? Thanks 🙏
@chrisbiscardi
@chrisbiscardi 11 ай бұрын
its a plugin called Toggle and I bind the inlay hint hide/show to C-i
@ewe-studios
@ewe-studios 11 ай бұрын
@@chrisbiscardi sweet, thanks
@Seacrest.
@Seacrest. 10 ай бұрын
Prefix|T> has to be in the same file (inline or module) to be able call has_prefix ?
@chrisbiscardi
@chrisbiscardi 10 ай бұрын
Traits have to be in scope to use their functions yes
@dragonmax2000
@dragonmax2000 Жыл бұрын
really neat, thank you for sharing
@hiraginoyuki
@hiraginoyuki Жыл бұрын
I like how you named the folder "tmmmmp" 😆
@chrisbiscardi
@chrisbiscardi Жыл бұрын
tmmmmp: for when tmp, tmmp, and tmmmp somehow already existed 😆
@haystackdmilith
@haystackdmilith Жыл бұрын
The only drawback of using generics is the rustc monomorphisation process… which takes compilation time as it has to generate functions for every type implementing PartialEq right?
@andr6192
@andr6192 Жыл бұрын
My assumption is that the compiler only generates it for concrete types that are actually being used in the code, but I don't actually know
@narigoncs
@narigoncs Жыл бұрын
@@andr6192 Yes, this is how it works. The compiler only generates it for concrete types where it's used.
@Shaunmcdonogh-shaunsurfing
@Shaunmcdonogh-shaunsurfing Жыл бұрын
Excellent video
@ExylonBotOfficial
@ExylonBotOfficial Жыл бұрын
This is very smart
@reaktoringhd
@reaktoringhd Жыл бұрын
Couldn't you use .any(|window| ...) Instead of .find(|window| ...).is_some()? I guess .any could be faster but I haven't tested it, can be that the compiler just optimizes the unused value away. But I believe .any is a little more clear what it is doing (well not a huge difference either way)
@chrisbiscardi
@chrisbiscardi Жыл бұрын
yes you could use .any
@yondaime500
@yondaime500 Жыл бұрын
This is actually a warning by default on clippy. warning: called `is_some()` after searching an `Iterator` with `find` note: `#[warn(clippy::search_is_some)]` on by default But it's listed under complexity, not perf, because it's just for readability and doesn't change the compiled code.
@no_fb
@no_fb Жыл бұрын
Rustlang? do you mean Rust?
@chrisbiscardi
@chrisbiscardi Жыл бұрын
Yup. There's a name conflict between the game Rust and the language Rust, so it can be helpful to differentiate, especially on platforms which have both topics
Scoped threads in Rust 1.63
8:57
chris biscardi
Рет қаралды 10 М.
Rust Generics
22:10
Doug Milford
Рет қаралды 21 М.
When you have a very capricious child 😂😘👍
00:16
Like Asiya
Рет қаралды 18 МЛН
It’s all not real
00:15
V.A. show / Магика
Рет қаралды 20 МЛН
VIP ACCESS
00:47
Natan por Aí
Рет қаралды 30 МЛН
99.9% IMPOSSIBLE
00:24
STORROR
Рет қаралды 31 МЛН
Rust Generics and Traits: Define Common Struct Behaviors 🦀
32:23
Trevor Sullivan
Рет қаралды 8 М.
Tricks of the Trait: Enabling Ergonomic Extractors - Rob Ede
31:52
Rust Nation UK
Рет қаралды 8 М.
Choose the Right Option
18:14
Logan Smith
Рет қаралды 75 М.
Rust: Generics, Traits, Lifetimes
35:34
The Dev Method
Рет қаралды 51 М.
Rust Structs, Traits and Impl
24:53
Doug Milford
Рет қаралды 34 М.
Rust Cologne:  Fn traits
27:12
Rust
Рет қаралды 7 М.
Why Can't We Make Simple Software? - Peter van Hardenberg
41:34
Handmade Cities
Рет қаралды 180 М.
RustConf 2023 - The Art and Science of Teaching Rust
28:01
Generic Types in Rust
15:18
Let's Get Rusty
Рет қаралды 75 М.
When you have a very capricious child 😂😘👍
00:16
Like Asiya
Рет қаралды 18 МЛН