Rust Pizza Slices, Arrays and Strings

  Рет қаралды 14,333

Code to the Moon

Code to the Moon

Күн бұрын

Пікірлер: 60
@aqdasak
@aqdasak 2 жыл бұрын
I think this is going to be the best Rust channel on KZbin. Your offerings are very crisp and concise with a good base. You are such a good chef.
@codetothemoon
@codetothemoon 2 жыл бұрын
Wow thanks Aqdas! I appreciate the kind words!
@jrmoulton
@jrmoulton 2 жыл бұрын
Second to this. I watch them all and this is the most clear channel I've found so far. High quality as well
@Cookiekeks
@Cookiekeks Жыл бұрын
@@jrmoulton have you watched no boilerplate?
@jrmoulton
@jrmoulton Жыл бұрын
@@Cookiekeks I have! I watch most of his videos. The rust content feels a little fluffy though. But that's just stylistic and a lot of people probably really like it
@kinositajona
@kinositajona 2 жыл бұрын
Basically, [u8] + "I promise this slice of bytes is a valid utf8 string" = str Vec + "...(same as above)..." = String Since we don't usually use [u8] without & or &mut, we also don't usually use str without & and &mut. Also, &Vec derefs into &[u8] via the Deref trait and &String derefs into &str via the Deref trait too.
@kiffeeify
@kiffeeify 2 жыл бұрын
Yeah, and don't forget: The length of the u8 slice is not neccessarily equal to the length of the string, as a unicode character in UTF8 may span more than one byte. Correct?
@codetothemoon
@codetothemoon 2 жыл бұрын
Sounds right to me!
@Turalcar
@Turalcar 10 ай бұрын
@@kiffeeify Depends what you meant by "the length of the string" as almost all str funcions (e.g. len()) operate on bytes.
@TwoDozenSigma
@TwoDozenSigma 2 жыл бұрын
I can strongly recommend this channel to anybody who's studying rust. The example is concise and easy to understand! Thanks!
@mageprometheus
@mageprometheus 2 жыл бұрын
Thanks. I've thought about this several times in the past and always lost the gist of it after a while. I found drawing out the memory structures and pointers, to get a proper understanding, helps me retain stuff better. Diagrams are also great for lifetimes. Back in the 80s, I didn't have any trouble learning C and C++, I suppose it's being retired and older.
@codetothemoon
@codetothemoon 2 жыл бұрын
Re: drawing diagrams to help retain things better - I'm exactly the same way. But for some reason I don't do it as often as I should.
@edgeeffect
@edgeeffect 2 жыл бұрын
I like the way that you've got your monitor high and to your right, so that when you're typing, it almost looks like you can see the code as we see it in the composite image. I'd think about moving your monitor a little higher, to emphasise this a bit more. I don't know if anyone else would ever notice but I WOULD. ;)
@codetothemoon
@codetothemoon 2 жыл бұрын
Nice, yeah actually the first time I did green screen it happened to serendipitously turn out this way, ever since then it's been deliberate. Good point about the monitor being higher, that makes sense! I'll likely be building out a little studio soon, so I'll definitely incorporate that...
@andrewdatar9880
@andrewdatar9880 6 ай бұрын
Great content, thank you! It would be much easier to follow with the terminal to the right side. When the terminal appears from the bottom, it obscures most of the code and it not clear which code is responsible for printing. Example 5:51
@codetothemoon
@codetothemoon 6 ай бұрын
thank you and thanks for the feedback!
@reaktoringhd
@reaktoringhd 2 жыл бұрын
Thanks, that pretty much cleared it up for me
@codetothemoon
@codetothemoon 2 жыл бұрын
nice, happy you found it valuable!
@DJenriqez
@DJenriqez 2 жыл бұрын
you should do video for every coding language, and every framework,... your explanations are amazing,...
@codetothemoon
@codetothemoon 2 жыл бұрын
Thanks Erik! I should wish I could! So little time, so many cool technologies to explore...
@unknown-tu4wx
@unknown-tu4wx 2 жыл бұрын
finally found thanks to the author
@codetothemoon
@codetothemoon 2 жыл бұрын
Nice!
@vansharora1512
@vansharora1512 2 жыл бұрын
Absolutely interesting
@codetothemoon
@codetothemoon 2 жыл бұрын
glad you liked it!
@hfuhruhurr
@hfuhruhurr 11 ай бұрын
That was both extremely helpful and extremely frustrating. Rust is weird. One day it'll sink in.
@mikehoughton204
@mikehoughton204 2 жыл бұрын
"A borrowed reference take a known amount of memory as it is ultimately a pointer.' But where does the 'thing' that it's pointing to exist? i.e [1,2,3]. I don't quite see how the size of [1,2,3] is not known statically and yet &[1,2,3] is. Is the size of [1,2,3] encoded in the pointer? (But if it is then it must(?) have been determined at compile time.) Could you explain a bit more please? Thanks
@codetothemoon
@codetothemoon 2 жыл бұрын
In this example, [1,2,3] is a literal and thus is included in the program executable, and I believe is loaded into memory alongside the machine code instructions for the program, so that's what the pointer points to. This is a little confusing - In this particular example we know the size of [1,2,3], and we could assign it to an array which carries a known size at compile time - similar to what we are doing on line 5. But because we are assigning it to a variable of a slice type on line 6, and the size of slices isn't known at compile time, we get a compiler error. Hopefully that makes sense :)
@mikehoughton204
@mikehoughton204 2 жыл бұрын
@@codetothemoon Thanks!
@igz
@igz 2 жыл бұрын
1:27, line 6. How are we able to borrow the array [1,2,3]? Who owns it? I thought everything that can be borrowed must have an owner.
@jeffg4686
@jeffg4686 2 жыл бұрын
Another reason for &String is that if you have an owned but the methods takes a slice, you can just throw the ref on there it will auto-convert it to &str - just shorter than .as_str() on it (which I'm not sure if as_str() is a compiler trick under the hood or if that's actually runtime method call (as it appears), but the & in front of String would (I believe) be all compile time, so it could possibly be faster too. Maybe worth a 1M call loop test to check if any performance diff.
@codetothemoon
@codetothemoon 2 жыл бұрын
Good point, yeah I'm not sure if .as_str() is equivalent to just using & either. I find auto coercion using the Deref trait pretty handy in general...
@josedavidmoya344
@josedavidmoya344 2 жыл бұрын
Are you using Producer edition?? What do y'all recomnd if I want to make s but not record my voice or tutorial?
@codetothemoon
@codetothemoon 2 жыл бұрын
Producer edition of what? I think the answer is no because I'm not sure what that is :)
@LuismaLorca
@LuismaLorca 2 жыл бұрын
Which theme are you using? Awesome video btw.
@codetothemoon
@codetothemoon 2 жыл бұрын
Dark+! and thank you!
@mohammedshehbazshaikh4683
@mohammedshehbazshaikh4683 2 жыл бұрын
Tell soft soft I love what they did with the $15 doallors one %
@codetothemoon
@codetothemoon 2 жыл бұрын
🔥
@professionalcat9928
@professionalcat9928 2 жыл бұрын
im hungry now
@codetothemoon
@codetothemoon 2 жыл бұрын
right?? I find it very difficult to talk about slices without having an image like the one in the thumbnail pop into my head...
@rainerwahnsinn3262
@rainerwahnsinn3262 Жыл бұрын
1:27 Isn't the value actually a reference to an array instead of a reference to a slice, and it only coerces to a reference to a slice because of the explicit type annotation? Edit: Same for the subsequent `slc_smart` declaration. It only ends up a boxed slice because of the explicit type annotation, since the value is a boxed array.
@keatonhatch6213
@keatonhatch6213 2 жыл бұрын
Correct me if I’m wrong but the slc2 where you said it doesn’t make a copy, that’s wrong. Integers implement the copy trait so any reference to an integer makes a copy hence why you don’t need the .copy() method on borrowed integers.
@QazCetelic
@QazCetelic 2 жыл бұрын
What is the difference between Rc and Box?
@codetothemoon
@codetothemoon 2 жыл бұрын
The short answer is that Rc is for when the data needs to be referenced from multiple places and should be cleaned up when nothing references it anymore, and Box is for when it only needs one owner and should be cleaned up when that one owner goes out of scope. For more info check out my video called "Rust's Alien Data Types" - we cover them more thoroughly there!
@kdcadet
@kdcadet Жыл бұрын
I am so confused after watching this, which is good. I guess I really didn't understand str and String before.
@codetothemoon
@codetothemoon Жыл бұрын
sorry about that! I've gotten quite a bit of feedback that this isn't the clearest explanation I've done. What part did you get stuck on?
@kdcadet
@kdcadet Жыл бұрын
@@codetothemoon My confusion, I believe, stems from mostly seeing Vec used in the book. I am also confused why `let a [i32] = [1,2,3];` would give an error. This seems exactly like the sort of case that the rust compiler would be able to figure out, similar to how `let x = 3;` works fine, since the compiler sees that 3 is i32 (assumed) and goes along with it happily.
@irlshrek
@irlshrek 2 жыл бұрын
I wish I could type that fast
@codetothemoon
@codetothemoon 2 жыл бұрын
All you need is a screen recording program, and a video editing program with a fast forward function! Voilà!
@pup4301
@pup4301 2 жыл бұрын
I use String for everything because of the limitations of &str.
@codetothemoon
@codetothemoon 2 жыл бұрын
Yeah I can definitely see the appeal in that approach!
@محمدرعدوبرق
@محمدرعدوبرق 2 жыл бұрын
Aweso tutorial but I dont have a snare anywhere on my list. Wtf
@codetothemoon
@codetothemoon 2 жыл бұрын
snare?
@AbhishekBajpaiHere
@AbhishekBajpaiHere 2 жыл бұрын
Usually your videos are very clear, this one for me was a little hurried and convoluted also this one lacked the explanation about which type one should use when designing APIs so that its easier for everyone to call. I know this I just hope this video also covered that so anyone stumbling on this video for rust strings is aware. Overall great content, i love watching your videos even if they are on topics i already understand. Video Suggestion: Actix Actor Framework
@codetothemoon
@codetothemoon 2 жыл бұрын
Abishek - thanks, this is great feedback, exactly the sort of thing I was looking for. When I saw the final edit I definitely wished I had gone into a bit more detail on some of the topics. Will incorporate these learnings into future videos! Re: Actix actor framework, good idea, I've added it to the video idea list!
@cristianance9349
@cristianance9349 2 жыл бұрын
L struggled to make soft
@codetothemoon
@codetothemoon 2 жыл бұрын
🔥
@sirwanisdrunk
@sirwanisdrunk 2 жыл бұрын
i need an soft soft crack how can i get it
@codetothemoon
@codetothemoon 2 жыл бұрын
I am not an expert in "soft soft" sorry!
Helix 🧬 the Rust Powered Development Environment
12:53
Code to the Moon
Рет қаралды 175 М.
Rust's Most Important Containers 📦 10 Useful Patterns
17:11
Code to the Moon
Рет қаралды 130 М.
人是不能做到吗?#火影忍者 #家人  #佐助
00:20
火影忍者一家
Рет қаралды 19 МЛН
Try this prank with your friends 😂 @karina-kola
00:18
Andrey Grechka
Рет қаралды 6 МЛН
Use Arc Instead of Vec
15:21
Logan Smith
Рет қаралды 155 М.
Understanding Strings in Rust - String vs &str
6:29
Federico Terzi
Рет қаралды 19 М.
Dioxus vs Leptos  | Rust GUI Wars #2
21:18
Creative Coders
Рет қаралды 10 М.
Rust Interior Mutability - Sneaking By The Borrow Checker
16:02
Code to the Moon
Рет қаралды 74 М.
All Rust string types explained
22:13
Let's Get Rusty
Рет қаралды 188 М.
Is Rust the New King of Data Science?
15:38
Code to the Moon
Рет қаралды 140 М.
Ace Rust Macros ♠️ the declarative kind
14:06
Code to the Moon
Рет қаралды 41 М.
Strings in Rust FINALLY EXPLAINED!
21:40
Let's Get Rusty
Рет қаралды 78 М.
The Downfall of Actix Web
20:25
Ben Awad
Рет қаралды 242 М.
Factorio teaches you software engineering, seriously.
21:27
Tony Zhu
Рет қаралды 2 МЛН