What is Rust?
with Prince Wilson
Looking for a way to stretch your brain and try something new? Come join Prince Wilson as he introduces us to Rust!
Topics
Resources & Links
Transcript
Captions provided by White Coat Captioning (https://whitecoatcaptioning.com/). Communication Access Realtime Translation (CART) is provided in order to facilitate communication accessibility and may not be a totally verbatim record of the proceedings.
Jason: Hello, everybody, and welcome to another episode of Learn with Jason. Today on the show, we have the one, the only Prince Wilson. Prince, how are you doing?
Prince: I'm doing phenomenal. It is a wonderful day. It's bright outside today in New York City. So, I'm just living it up.
Jason: Yeah, I'm so excited to have you on the show. We've known each other through the party corgi Discord and various other channels for a while and you are a walking embodiment of a ray of sunshine. You are anthropomorphized joy. For those of us not familiar with your work, do you want to give us a little bit of a background on yourself?
Prince: Yeah. So, how do I describe myself in like the shortest amount of ways? I'm a software engineer that works at a company called Newsela, an ed tech company in New York, creating content for students, and specifically focused around news. One of the things, why would I want to teach kids about the news? That's such an important skill to be able to read the news and know different things that are happening in the world.
Jason: Yeah.
Prince: We do things about Fortnite and whatnot. You might be surprised, news.
Jason: Cool. What about outside of work? You are so much more than a job, Prince Wilson.
Prince: That's a good point. Outside of work is primarily still doing coding things. But I also read a lot of comics and play a lot of video games. My current video game of choice right now is Ghosts of Tsushima, a PS4 game.
Jason: I think I was watching you play this, were you fighting ghost monkeys?
Prince: No, that's Sekiro. Ands that as a Samurai game. Very close. I have a theme, Samurai, I really enjoy those types of games.
Jason: Nice, nice. Yeah, so, today we are gonna be doing code things.
Prince: Yes.
Jason: I think we -- we also have, if you're interested in such things, a party corgi game night chat. Happened last night where we played. Prince was there among us which is a like space what would you even say that game is? What's the point of that game? To lie to your friends?
Prince: It's like Clue, but in space. And people die more frequently.
Jason: Yeah. It's pretty rough and also I'm very bad at it. Benjamin is in the chat. I am the imposter, it was obvious from the start. (Laughter)
So, what we want to do today not about gaming, we want to do a little bit of Rust. This is going to be really fun because I have written zero lines VI Rust in my entire career. I've also written maybe under a hundred lines of typed language code in my entire career. I've never been a Java Dev or gotten into any of those things that like when I look at Rust, it kind of feels reminiscent of that sort of thing and I hope you can talk about that origin. What I would love to hear from you, what got you into Rust?
Prince: Well, there was a Tweet. Basically like, I want to learn another language. First I was, like, I'm going to pick up Python because I use that at work. But I want to decouple what I'm doing outside of work with things that interest me. One thing that really interests me right now is about system-level programming. That's kind of what led me to Rust. I didn't want to write C code. That was my first language, terrible experiences writing C, and I don't want to do that again. Not to shame those writing C, you are amazing folks. Rust came through and I was like, wow, that is a lot of fun to write.
Jason: Yeah, awesome. There was a question in the chat.Ed a about the vod, it's on the party corgi network Twitch. We do those game nights regularly and they are very silly. So, like with Rust, Rust has -- it's got a big following, right? And I'm noticing a ton of people are really getting excited about it. What would you like if you're gonna -- you're choosing Rust. Rust is a -- a backend language? A frontend language? What -- how -- what would you use it for?
Prince: Yeah. That's a great question. I think that, like, for the most part, you're gonna see it in several different places. I could you could kind of say it's a backend language. The idea is that it can like describe to how to manage like requests. There's a lot of tooling around making web frameworks as well inside of Rust.
Jason: Okay.
Prince: But the tool is applicable for things like commandline interfaces. There's also a huge wave within WebAssembly and how that works with Rust. And I think that's where we see a lot of overall kind of progress with Rust being a kind of the staple to how can we make JavaScript even more powerful than it currently is? And then you'll also see it in embedded systems and system-level programming. It's multi-purpose.
Jason: You can go all over the place, right? I think I saw Chris also in the part corgi Discord, he's doing a Rust. Not even really a prototype anymore. It's a thing he's building of doing a static site generator powered by Rust. And that is being distributed -- like is that -- is it building to WASM? Is that a thing people are doing with Rust?
Prince: That is a thing that people are doing with Rust. That particular tool isn't what he's doing.
Jason: Okay.
Prince: But I think that a lot of the things that he's doing is learning how we can take some of the -- kind of the benefits of Rust, like some of the strictness that it has, to describe some of the things that he wants to be doing for his static site generator. That's where I think we can see kind of benefits in that way.
Jason: Yeah, yeah. Very cool. Okay. So, I guess maybe the best thing to do, then, is just to get right to it, right? So, let's switch over into pairing mode and let's see if we can write some code. Before we get started, let's do a quick shoutout to the sponsors. We are getting live captioning right now as we speak by White Coat Captioning. Thank you so much to Amanda who is with us today. You can go to LWJ.dev/live to see those captions. Those are made possible by Netlify, Fauna, Sanity and Auth0 who all generously chip in to make the captioning affordable for us and make this accessible to more people. Super excited about that. A sub from Kaleigh, thank you so much. I also a saw before we started that Ben upped his subscription. So, thank you both so much for the sub. I guess Twitch is doing something right now called Subtember, I guess means you can get a discount. I really wish I could use the boop emoji and bury Jason in print, you could do that right now. And you could get it at a rippin' discount, I think. (Laughter) Speaking of shameless self-promotion, follow Prince on Twitter. He is delightful. That being said, let's talk about Rust a little bit more. Here is the Rust home page. And here we go with the boops. It's comin'.
Prince: Just getting started!
Jason: Thank you for the sub, very much appreciated. So, Rust, it is a language empowering everyone to build reliable and efficient software. I like that promise. If I want to get started, you know, I should have asked this before we were live, do I need to install a bunch of stuff to make this work?
Prince: So, there's a yes and no. We can install things, it won't take long.
Jason: Okay.
Prince: Similar to code sandbox, there is a Rust playground as well. We can do either one of those. Installing is totally fine, there's nifty tricks we can do along the way.
Jason: Yeah, let's install it as well. And Eco, thank you for the sub. Holy crap, one year of you been subs! It doesn't seem like I have been doing this show for a year. Will, let's go. Jimmy Miller is in the chat making all sorts of Rusty jokes right now. No tetanus here, we are just writing code. We're also going to do a fun thing where we have done collaborative code. So, if I want to get started, what should I do to get Rust running on my computer?
Prince: So, if question go back to the Rust lang page. There's a big yellow button, I believe, that says getting started.
Jason: Oh, yeah, that one. Got it.
Prince: So, we can click that button and it's basically like, cool, you can get started without doing the thing or you can do this commandline script. Always love, you know, couping and pasting random scripts into the system.
Jason: Yeah, yeah, whatever. Copy/paste. It's gonna be tune. Oh, thank you -- I'm gonna get your name wrong. So, I'm just going to say thank you. Appreciate the sub very much.
Prince: So, when we install this, what it's going to do is install what's known as Rust up. It's like the nvm or Rust manage. It does other things, we'll go with the default. The number one.
Jason: Default, here we go. Cargo -- oh, my god, did you just install Clippy on my computer?
Prince: I did.
Jason: Excellent. You know what I found yesterday that I'm gonna show you? What was that? It was like --
Prince: I already love this. WordArt. It's already starting off great.
Jason: Make WordArt. Look at this. So, this is a website that I found. And you can make your own WordArt and it is just delightful and I would highly recommend that everybody goes and plays with it.
Prince: Can you -- can you like export your WordArt?
Jason: I think so. Let's find out. No. Maybe. I don't know.
Prince: That's so cute.
Jason: Oh, yeah, you can do something.
Prince: Share on Facebook. I would --
Jason: This.
Prince: Little thing.
Jason: Beautiful. It is beautiful. Yeah. All right. Yeah. So, this is great. Yes, we -- Alex, you're late. You're too late. We're done. This is it. That is what we did today. Is WordArt. We're done, everybody, pack it up. We're going home. (Laughter) All right. So. I have installed Rust. And now that I have installed Rust, I should -- I should probably actually run this, right? So, that we've got Rust in the thingy?
Prince: Yes, let's do that.
Jason: Okay. Excellent.
Prince: A way to test it always, you know, Rust C. And we can do space, hyphen, hyphen version.
Jason: Like that?
Prince: Yep. Exactly.
Jason: We got it. We have -- we have a Rust.
Prince: So, for context, this is the compiler. So, one was major differences between something like JavaScript and something like this is that it's a compiled version. So, we'll do a step where we take our source code and we create an executable that we have to run. So, we can like start with a very basic file and then like work our way up to what you would sigh like in a traditional like Rust project.
Jason: Nice. Okay. Yeah. Very cool. So, do you want me to start in this live share here? Or should we --
Prince: Yeah.
Jason: Or start in the commandline?
Prince: Let's make a new file. Let's call main.rs. That .rs signal that is we are writing some Rust. See in VSCode, it is ready to go, it knows it's a Rust file.
Jason: I even had Rust syntax highlighting installed. I did not I had done that.
Prince: I don't know if that was prebuilt for VSCode or where it came from. There's also other extensions that exist that I can share later that might be helpful and nice.
Jason: Nice. Okay. Perfect. All right. I've got my main.rs. And I guess this is my first time writing Rust, so, I guess we have to do the obligatory hello world.
Prince: Exactly. To be fancy, do hello chat to empathize with what we were doing before with the WordArt.
Jason: Okay. Perfect. To start, like -- we don't have like an echo command or anything, do we? Like, I can't just be like, hello, chat?
Prince: Nope. Ever Rust project, everything is going to need a main function. Similar to like what you see in C. You're going to need to have like a main function. To write our function, we'll have to use the keyword fn and then the name of our function. So, main, and then put parentheses afterwards and then we'll use our curly braces. Similar to what you see in other languages when writing your functions. But you'll see fn keyword. And then you'll say println, and then an exclamation mark. And an emphasis, and give it a string of whatever we want to paste out.
Jason: Okay.
Prince: So far it feels pretty ergonomic, right?
Jason: Is this to say you want to run this function?
Prince: So, it's a special type of function. So, not every function is gonna look like this, but this one in particular, print, we'll need to do this way.
Jason: Okay.
Prince: And then before finish, we just have to add a semicolon after the println. That's the only caveat with down some Rust code is we're going to need to do a bunch of semicolons. Better be ready for it.
Jason: I'm ready for it. I love to do semicolons, I usually let it do it itself.
Prince: There is a Rust format, I think it does it for us, I'm speaking out of -- but there is a prettier equivalent built into Rust that is cool.
Jason: Oh, that's super-cool. Let me make sure that I have it here. Rust C version.
Prince: Great check.
Jason: Do I execute this or compile it first?
Prince: This is always going to do our compiling step first.
Jason: Okay.
Prince: Rustc and then give it the file that we want to compile.
Jason: Ooo... wow, it did all that stuff and then it did no stuff.
Prince: Right. It does a bunch of stuff for us and then takes it all away. You won't be able to read it inside of VSCode. But you can try to open it anyways and see what happens.
Jason: Let's see what happens. Let's see what's in here. Oooo! So, is this like straight machine code?
Prince: Yeah. It's a binary file. So, it will do some executables for us. So, now we have this executable. So, we run it like every script that we do inside of our terminal. We use like the period/and then the name of our files, which is just main.
Jason: We did it.
Prince: Congratulations. You're now a Rustatian.
Jason: Does that mean that I can do the ye claw?
Prince: Yes, you can do the --
Jason: Yee claw! (Laughter)
Prince: Oh!
Jason: All right. Somebody clip that because we are going to make that into a command for the channel for the one other time in the future that I'll ever say --
Prince: I was not ready for it.
Jason: Okay. Just take a break, everybody.
Prince: I was not ready for it. That was --
Jason: Alex in chat is saying --
Prince: That was scripted for somebody else. But it was not scripted for me. I had no idea.
Jason: Here's the thing, I have been just been watching everybody have a great time with all the Rust jokes and I have been feeling left out. Now that I've written at least some Rust code, I feel like I can be included. There's a lot of pent up Rust envy that came out in that yee-claw.
Prince: That was amazing. That was so funny.
Jason: All right.
Prince: I'm still like reeling. (Laughter)
Jason: Okay. So, I'm ready to roll. Like let's do some more. So, at its surface, like, this immediately feels familiar, you know? As someone who has written various programming languages, you know, we're doing all the same things, we're declaring functions, we're naming our functions. We have the ability, it looks like, to pass in arguments. And we've got whatever the equivalent of the print or echo or, you know, whatever command. I'm gonna have to look up exactly how that works, I get it, but I don't get it.
Prince: This is a special function for context.
Jason: Got it.
Prince: It's a called a macro. That's the reason it doesn't feel similar, because it does specific things for you.
Jason: Gotcha. Excellent, excellent. Cool. So, like, I'm ready. I'm ready to do more stuff. So, what should we do next? Thank you very much unmarkedvans. Unmarkedvans? That's a spooky Twitter -- or spooky Twitch username. Thank you nonetheless for the sub. Enjoy the boops, Spam them at will. I thoroughly expect you to drown Prince in boops after this ends. Preferably after we get through coding things so people can still see the screen. Okay. Where would you like to go from here?
Prince: So, let's -- let's start by making a new project? So, like I mentioned before, we installed Rust. And we use -- the script that we ran installed program called Rustup. A Rust version manager. In addition to that, it installed a bunch of other stuff, one of those things is cargo, which is the package library kind of built into Rust.
Jason: Okay.
Prince: So, in our terminal, what we'll do is we'll say, like, cargo, perfect. And we'll say like cargo new and name our project. This is the equivalent of starting a new npm project.
Jason: Oh, nice. If I target this folder will it break because the folder is not empty?
Prince: Let's find out.
Jason: No. Oh, wait, I can cargo init this directory. Let's find out.
Prince: Let's find out.
Jason: What did it do? Is this what we wanted?
Prince: Yes. Yes. I don't see your file tree. Where are the other files? But, yes --
Jason: Oh, sorry, this is very cool. It already found the main.rs, which is awesome. It picked up a name. It -- this is -- yeah. This is very cool. And so, actually, does that mean that now it's gonna change the name that we execute to what is Rust?
Prince: I think it does. We could start over --
Jason: Oh, no, I want to see. Does it do it?
Prince: It's not going to -- it might not work right now. But one thing we can do is --
Jason: Okay.
Prince: Cargo does the build/run/test kind of project for us. Similar to what you would do inside of in. It doesn't have customizable scripts like you would want. But let's do cargo run. Let's do that instead.
Jason: Cargo run. Compiling.
Prince: Yep. So, it does the compilation step for us and then it will tell us --
Jason: Wow! And it ran
Prince: It did. I did not know it was going to work. I love it. Love when it does the thing.
Jason: Super, super exciting. This is very, very cool stuff. So, it did build it out as what is Rust. And do you get a song -- I think I'm a lot older than you. The song -- what is Rust? Baby don't hurt me (singing beautifully...) --
Prince: I know what you're talking about.
Jason: Do the rest of the episode like this.
Prince: Yes, I wasn't sure if I was in the opposite direction. And make sure to do it that way first.
Jason: Perfect, okay, excellent. Stainless steel. What are you talking about, Yash? I don't know what that means? So, let's do, so, we've built a project, and as you said, it looks -- it's obviously written in TAML which is TAML, but it's familiar. Like it looks like what I would expect in a package JSON or any other kind of declaration file. We've got names, versions, who wrote it. Is that the name of the -- the Rust edition? Or I don't know what edition means.
Prince: Yeah. Think of it like that. What edition of Rust did it come through with?
Jason: This is the input file, this is the output file, which is pretty exciting. And also looks like we could, if we wanted, install dependencies.
Prince: Yes, and similar to npm, I want these dependencies. We have to explicitly mark them ourselves. Oh, can't just write an npm install and it just works.
Jason: Okay. Do we want to do that? Or what do you want to do first?
Prince: Let's hold off for writing any dependencies first. Just because we have to explore more of what is there.
Jason: Okay. Yeah. Let's do it. So, let's see. There's a comment in the chat. Usually the sources are in source the so the bin is only necessary because that is not the case.
Prince: Yes.
Jason: Does that mean if I make a source folder and I move this in here and I was like, get out of here, and then I was like cargo run, it does work.
Prince: Look at you. Taking all of why knowledge and putting it together. Like that -- like that's awesome. Like I didn't know, one, that I could change like the bin and whatnot. So, we both just learned something together.
Jason: This is very cool stuff. I'm pumped about this. This -- I realize that I am so, so in the shallow end of this pool, but I'm like, let's write everything in Rust. (Laughter) Okay. So, let's do something -- let's do something else. Let's dig into whatever you want to do next. But let's write some Rust.
Prince: All right. Let's start with just some types.
Jason: Okay.
Prince: That's probably the thing that we can elevate towards. Let's make a variable.
Jason: Okay.
Prince: Keyword for making a variable inside of Rust would be like let. Everything has to exist inside of a function. Let's put that inside of our main function.
Jason: Got it. Got it.
Prince: And make it like age. And this is my age. We'll give it we'll say equal to, I don't know, I'm giving you a random age, this is not my actual age. 10. Let's say that.
Jason: Okay.
Prince: And then we'll do our semicolon, perfect. Now, if we do this and save it, and then try to do cargo run. Let's see what happens.
Jason: Ooo -- it doesn't like it.
Prince: It doesn't like it? Why do you say it doesn't like it?
Jason: It says we didn't use the variable.
Prince: Right. This is just a warning.
Jason: Oh, cool.
Prince: But that kind of goes into Rust is already kind of intelligent, it's kind of baked in with telling you, oh, hey, there's some like things you can do to improve our code. So, we'll have our variable. And then let's, like, print out that variable. So, like my age is this thing. And so, we'll start with println, perfect. And we'll say, my age. And you're probably like, oh, how do I do interpolation? And the answer is similar -- I don't know if you ever use Python. It kind of feels similar to Python where we have like curly brackets inside of our string and then we'll have a comma afterwards.
Jason: Like that?
Prince: We need to close it. And then a comma outside of this string.
Jason: I've never seen this before. Okay.
Prince: So, I'll take that out and move it here. And we'll do age. Like that.
Jason: Oh, I understand. Oh, oh, oh. We're defining like where it goes.
Prince: Yeah.
Jason: And dropping in. If we had two, would we do another one and then like another argument out here?
Prince: Let's start with doing the first one.
Jason: All right. Fine, don't let me get ahead of myself, I guess.
Prince: No, I think asking those questions is a good thing. Perfect.
Jason: All right. Here we go.
Prince: So, let's do -- let's do this. Let's do -- I'm trying to think of another like silly variable. Like let's say -- let's add a new variable color. And this is going to represent your favorite color. So, we'll say like let color equal and then we'll do a string of whatever thing that we want. Let's do it above so that we can do it in the same println to test what your question was.
Jason: Okay. Right? Right?
Prince: Nice.
Jason: All right.
Prince: And so you can -- yep! Perfect.
Jason: Let's see what happens when I try this. Can I spell chartreuse twice in one episode? Stay tuned to find out. Nope. So, it doesn't like this because it is not found in this scope. Which means that my -- my completely uneducated guess about how this might function was incorrect.
Prince: Actually, I thinking that from what it says, it says chartreuse is not found because our variable is called color. But I'm glad that you also read the error. So, that also was like, a good, like, process. That was --
Jason: Okay. But let's take a second to point out that I read the error and got exactly the wrong takeaway from that. (Laughter) Okay. So, let's try that one more time and see now. Oh! It does just work.
Prince: Yep
Jason: I would have totally --
Prince: You were 100% on it.
Jason: I was excited about trying to spell chartreuse.
Prince: I'm excited you got it right twice because I don't know how to spell chartreuse.
Jason: It's because there's booze named after it, that's got to be what it is. Okay. So, yeah. Chartreuse, it's a great color. It's also --
Prince: It's a yellow-green color.
Jason: It's a great digestive kind of thing. It's a French liquer.
Prince: I had this question once, what is the color chartreuse? They gave you the colors red, purple, or yellow-green. It clearly sounds like a purple and I was baffled to know it's a yellow-green.
Jason: The color is lime. Incorrect, Nicky. It's very different. Slightly different.
Prince: So, what this kind of lets us see is that we did variables and we talked about different data types that are available right now inside of Rust which I think is a good important thing to go through. You can see here we have integers and we also have strings. Now, things to note is that with Rust, integers come in different formats.
Jason: Okay.
Prince: So, it kind of just infers what you needed a will. But specifically here, the kind of -- you might have heard of this in computers, is that values inside of programs take different sizes. So, you can actually specify how large this integer needs to be available for, what size it can handle. Or it will cause what is known as integer overflow and we can cause that if we would like to.
Jason: I would like to.
Prince: All right. Let's do it. So, like by default -- I actually don't know what the size is, what the type is. It's inferring what the type is for our integer online four.
Jason: Okay.
Prince: We can specify the type by using colon and whatever type we want.
Jason: Okay.
Prince: Let's say we have an integer. Let's say it's an integer of, I don't know, 8.
Jason: Okay.
Prince: I8. Which represents it can go 256 bits.
Jason: Okay.
Prince: So, this is like 2 to the power of 8 is how you can do that. You can throw out a number.
Jason: All right. I done broke it. Let's make this happen.
Prince: And you'll see, cannot compile.
Jason: The literal 1,000 does not fit into the type i8 whose range is negative 128 to positive 127.
Prince: Right.
Jason: That is very cool.
Prince: The reason for that is, one, is because we're using an integer. So, that means that we have positive and negative numbers. So, I can use 256 between both ways. And --
Jason: Right, right.
Prince: And basically know, how many -- how much numbers do I want to have space for? If we want to do an unsigned version, replace the i with the u.
Jason: Then it would be 256, okay.
Prince: This range is zero to 255. The other is negative 128 to 128.
Jason: By setting this as 8 bit, there's some interesting chat stuff going on. Because it's -- it's like how many values you get in binary. So, 8 bit is -- 8 bit's just this, right? Because it would give us 256?
Prince: 8 bits are --
Jason: Is it straight it up 8 bits?
Prince: Yes.
Jason: You can only get to 256 with this? That doesn't seem right. Just one, two, four -- well, maybe that is right. I don't know. I'm bad at binary. Anyways. Things -- like, I'm like, hey, let's take a moment to learn. Actually, I don't know this whole answer. (Laughter) If somebody's got a helpful video in the chat, please post it so that we don't leave people super-confused.
Prince: It's i32 by default. Thank you for that.
Jason: I -- oh. Yeah. We get a 32-bit integer by default. That's pretty big. Probably fine.
Prince: That's a standard size. You probably want to do that. I didn't want to speak out of turn because my intuition is i32. I shouldn't say absolutes because I'm always --
Jason: So, if I take this, right? And I just keep going. Let's see how far we can go. Is it going to infer that it needs more? I actually don't flow if --
Prince: Let's find out.
Jason: If this is bigger than a integer. Let's find out.
Prince: It definitely is, it's very large.
Jason: Integer literal is too large. So, for that, I would need like, whatever. Will it let me do something like that?
Prince: For context, i32 or specifically 32 bits is like, what? 4 billion?
Jason: You can't use that.
Prince: Yeah. It's definitely not enough. Yeah. And u128 is the maximum. And that's a very large number. I don't know what kinds of programs we're writing that are using ui28s.
Jason: I'm trying to calculate space distances in inches here. I get it. This makes sense. We've found the limits of reasonability for numbers.
Prince: If you want a context of how it works, right? Is like 2 to the 8 represents 8 places. So, we have those 8 numbers.
Jason: Okay.
Prince: And the 2 represents like if you think about it is the two options you have, either a zero or a 1. That's how you can keep track of that in your noggin.
Jason: Got it, got it, got it. Yeah. That makes sense. And if I had a calculator handy, I would be so set for that. (Laughter)
Prince: In case you're like, you know, give myself a trivia question before the compiler tells me.
Jason: No, it's perfect. It's perfect. Okay. So, we've got this. And it will tell us if what we're trying to do doesn't work. So, if we like specify that it's an 8-bit integer and we go above that, it will say that we can't. What happens if we try to do something that obviously shouldn't work. I want to multiply by age by this color.
Prince: Let's try it.
Jason: Okay. How do you do math in Rust?
Prince: The same as other languages. You can just do like age and then the asterisks and then color. And then you get --
Jason: Okay.
Prince: In this case, we're not in taking its value. But it's probably going to be like, compiler error, I cannot do that thing.
Jason: Okay. This is no implementation for integer multiplied by what I'm assuming is like a casting, that would trike to force it to an integer, is that right?
Prince: I think from reading this, yes. It's like trying to do multiplication with a string. And so, that -- that is the reason why you see like the lower case STR there. It's that string.
Jason: Got it. And so, what if I did like a decimal of this, and I just try to do like -- because that's -- that's a floating point number, not an integer. So, it doesn't like that either. And that would be -- we don't have an implementation for floats an integer. So, if I wanted to do that, I would do what? Cast that to float?
Prince: Well, similar to the syntax before, it's just like the f and we probably still have to say the size.
Jason: F32.
Prince: Let's try that. See what happens.
Jason: Is a float literal.
Prince: And so, you'll see here the error is like, I can't do the thing that you're asking me to do cause you're not really a float. It's just -- just cause you have the 10 doesn't mean --
Jason: Anything can be a float if it believes in itself is really -- okay. So, basically, if you want to do this, you would need to like force it to be a float. Okay. All right. That makes sense.
Prince: And that's kind of true with a lot of languages, right? There's certain boundary boxes that we set because we're like, cool. This is describable. I can describe to you how to do an integer times an integer or a float times a float. Some languages aren't able to do that coercion. That's usually why you have to think about them being the same type. Or explaining how they should resolve it.
Jason: This is cool. It is helpful that it's explaining this as we go as opposed to just, you know, saying, like, invalid type or something. Which is -- which is helpful. So, all right. So, we're -- we're getting a warning now. I also really like these warnings. It's like, hey, you didn't actually use that. Did you need code?
Prince: This is one of my favorite things about Rust, it is good at explaining to you, hey, I see this thing that you want to watch out for or you're doing this erroneous thing all together and we don't want you to do that thing.
Jason: Can I just do something like this, does it work?
Prince: Let's find out. I'm very much, let's find out and let's do the thing and we'll see what happens.
Jason: Fine.
Prince: And you'll notice there's like helps, warnings and then errors. It is even good at telling you, hey, you might want to fix it doing this thing.
Jason: That's cool work though, that it lets you -- and the fact that it told me that is very cool. Because otherwise, you know, if I had tried to do this in, you know, JavaScript or something and it was just -- it's just uncaught error.
Prince: Yeah.
Jason: And then you have to Google the uncaught error. So, showing me, hey, that's what you should be using, that's really powerful. That's really nice.
Prince: Yes. And someone in chat has mentioned as well that you can do quick like printing if you want using debug or dbg exclamation mark. And that's another one of those kind of nifty tools that you can have.
Jason: What if I try to make it do stuff that it doesn't want to do? Take two integers, it's probably just going to run, right? Yeah.
Prince: Magic. It even told you that this is an expression with a thing. And it does like what the evaluation is.
Jason: That's really nice. I think that -- that is -- that is a very handy -- that debug command is amazing.
Prince: Yeah. That's like really nifty. And I didn't know that it could do the expressions out. I've used it for telling me what a value is which is really nifty when you're doing a bunch of programming. I don't know what the thing is that we're looking at right now. Let me know what the thing is.
Jason: That is handy. When I log in java script or whatever, I wrap the thing that I'm doing logging in an object so that I get the name of the variable and then the value. And the fact that this just does that by default shows you the name of the variable and the value. Or in the case that it passes something raw like the expression, it passes the expression and the outcome. That's cool. I really do like that.
Prince: Yeah. There's another small thing you can do inside of your print lines. So, in between the curly braces, that you can do a colon exclamation mark. And this is another similar -- exclamation instead of -- sorry. Question mark. I can't --
Jason: Question mark. Okay.
Prince: And this is a similar effect as well. Is it will tell you what the kind of like value can look like. It doesn't tell you the expression -- no, you did it right. It looks the same way. So, if something can be displayed in a certain way for your debugging purposes, you can use this syntax as well. But it will output like it's supposed to. And it will come in handy when you're looking at another data type called structs.
Jason: What's that? Should we do one of those?
Prince: Yes. Let's start with a tuple and build up to a struct using all the same things. We can get rid of everything inside of main function and start again with another data type.
Jason: Okay. What if I just --
Prince: Perfect. You can see here, you can comment using the double slashes. The single slash and star is also valid in here. That's good.
Jason: Oh, nice. I'm cheating by using the VSCode short code for commenting so it will tell me what to do.
Prince: I do the same.
Jason: I love learning new stuff.S in so exciting. All right. Let's do a tuple. And so --
Prince: Oh, wait. A tuple? Or a tuple?
Jason: Did I do it wrong? I don't know. I've never said that word out loud before. Honestly.
Prince: Both are valid. And I'm glad that you said it the other way.
Jason: Here. I'm just going to collapse that down. So we can still see what we're doing but we don't lose that code for later.
Prince: Perfect.
Jason: Tupleware. (Laughter)
Prince: This show is just about puns. That's really all we're learning with Jason today.
Jason: Very into it. Chris is here. What's up, Chris? Did you see my yee-claw? It was a good one? Wait, why? No, go back to being collapsed is what I want you to do. I'm ready. Let's do a tuple.
Prince: Cool. Once again, we're going to do our variable. Let's make it like an RGB. That's the go-to that I have always in my head for tuples, or tuples. And then what we'll say is colon and then parentheses.
Jason: Like this?
Prince: Yeah. And we can put a space before the parentheses.
Jason: Oh, before the parentheses, got it.
Prince: Yes. And then we want to describe all the values inside of this tuple. So, I would love to know what your -- so --
Jason: I don't know what that color is, actually. (Laughter) Let's see. It's red -- so, that's like an orangey-yellow, I guess?
Prince: Yep. Yeah. Red-green. Hm. I orange-yellow. I like it. So, we have the value here, right? Let's do this. And we'll see what happens. Let's print that thing again.
Jason: Okay. Cargo run.
Prince: And so it's --
Jason: And it says.
Prince: No warnings or anything. We can use the DBG command. We can say dbg and then rgb. And let's try that.
Jason: Okay. So, it is printed out our list.
Prince: And congratulations! You made a tuple. That's really all there is to access the values inside of that tuple, we'll use the dot notation. To get the element, you would use like dot 1 and that will give us the output there.
Jason: Okay. All right. This like makes sense.
Prince: Cool. Feels pretty good. Tuples, similar to other languages, once they have been created, it had exists. Like this is just like how --
Jason: Oh, my god. Oh, my god. Prince.
Prince: I thought it should be corgo instead of cargo.
Jason: What? No. That works. We got --
Prince: Can you do that?
Jason: We have to do very important work right now.
Prince: Or does it have to be a string. Alias corgo equals string, corgo. Yeah. Like Chris has in the chat.
Jason: Yes. Ah! Now we can do corgo run.
Prince: Oh, my gosh.
Jason: Very proud of myself. Okay. So, we -- good idea, Chris. Well done. Yeah. So, we're good. We're good. We can now do corgo. I'm gonna keep using cargo for anybody who tunes in later so we don't confuse the holy crap out of anybody. But that does make smile. Originally you said we would use this.
Prince: Yes.
Jason: Fundamentally, what's the difference here? What's changing when we make that switch?
Prince: Yeah, what I was going to get you to do is get you to do the version that has it with the type. Remember how we mentioned everything in Rust has a specific type. So, a lot of the time it's inferred. But let's say we wanted to explicitly say what we can and can't have in here. Our types in here. This is like representing the shape.
Jason: Gotcha.
Prince: And the values we would like to have inside of here. In our cases, similar to the rules we have above, we can only have certain size integers inside of here.
Jason: Okay. So, if I run this, that does what we expect. But then if I were to make this a negative number, because these are unsigned, it would yell at me because we can't have a negative value. That's cool. That's really cool. So, now we've got the ability to with relatively low effort validate input.
Prince: Exactly. That's one of the advantages with a type language. You're describing ahead of time for anybody coming in, these are the rules that you have to be following in order to make the code operate. And I think that kind of gives you a lot of leverage when you're talking about using libraries and packages. You can already say, I only accept these types. Unlike in JavaScript, you have to be like, cool. This is the language that we all agree on for things we can pass in.
Jason: Yeah. I'm just kind of messing with this a little bit to show that as we change it -- so, like, we can set this to be any -- any amount of tuples, right? It can be whatever. And if we don't follow that format, it yells at us. So, if we add extra stuff, it looks like this is -- that's inferred as an integer and that's why it's in the curly braces. So, we have to match our -- oops. Whoa, whoa, whoa. Calm on down there. We have to match our input to our types.
Prince: Yes.
Jason: But these are completely arbitrary as well. I can make this whatever I want.
Prince: Yes. So, as long as you describe to whatever type that you want to have, then it's totally fine.
Jason: Excellent. This is very cool. So, I'm -- I'm into this. Like this is super-exciting. It feels like all the things that you want from TypeScript. You know? Like you feel like people talk a lot about how much they love TypeScript, and it's because of stuff like this. You're getting these really clear definitions. But what I'm understanding here is that with this, you don't have that like any type that would just kind of let you opt out of typing all together. You have to make sure that things are type safe to function in Rust.
Prince: And that's one of the kind of differences between something that is by design a type language as opposed to a type system on top of a language. Because, one, you can opt out for TypeScript. And I think that in itself kind of -- it's not a problem. But it doesn't give you the same kind of strength because it's an easy opt out. It's an escape hatch. So, technically, you can be able to say, cool, I'm just going to write JavaScript again even though you're using TypeScript.
Jason: Yeah. Yeah. Man, this is cool. This is very cool. So, all right. So, we've got tuples. And when we're using these, so -- this is kind of like the long way. And I can see this being wonderful as you get started. But if you wanted to do like let's say a -- a person and this person was gonna have like a name -- or, sorry. So, this person's going to need a name. And an age. And --
Prince: That's in seconds.
Jason: In seconds, yes. Was literally born yesterday. No. Just a couple of minutes ago. And that's enough. So, then, in here, I would do -- what's -- is a string like str?
Prince: So, there's technically two forms of string.
Jason: Okay.
Prince: And one of them is someone in chat had already mentioned is called a string view. It's also known as a string slice. So, use the ampersand in front of the str.
Jason: Okay. And what would the -- what is that mean?
Prince: So, in this case, it's a signifier for like a hard-coded string you can kind of think of. This is a string known ahead of time of, I know its size. I can move it and it's fixed in size.
Jason: Okay. And I do that because we're not gonna -- so, if I was gonna change this and like eventually add my last name, then that would break this. Because it would be a different size, right?
Prince: Right. You wouldn't use this type.
Jason: Okay.
Prince: And that ampersand, once again, it -- it means something else. Which we will get to.
Jason: Okay.
Prince: There's like a lot of small things in Rust that happen. And I don't want to throw you into that part until we get to it.
Jason: No worries. Now I've defined my person. Right? And if I run this --
Prince: It's gonna break.
Jason: It broke! Why couldn't I -- okay. Let me see if I can figure this out.
Prince: All right. Wait, read back -- that's an important error there.
Jason: I have mismatched types. It expected a tuple because of default return type.
Prince: Yes.
Jason: Okay.
Prince: So --
Jason: So, I bind a tuple.
Prince: Yes.
Jason: And then I gave it what I thought was a tuple. Oh, wait, I'm missing a bunch of semicolons.
Prince: Yes. Exactly. Cool.
Jason: And now it works. Okay. So, does it tell me here that I didn't... oh, I started doing stuff.
Prince: So, you did a bunch of good things. The things that you saw in the errors, and this is like one of the errors that tripped me up for a really long time, and you'll see it a bunch of times depending on what you're doing. If you go back up to the first error, so, still a little bit more. Cool. That --
Jason: Oh, there's -- oh, it told me up at the top. I didn't go up far enough.
Prince: So, it tells us a few things. It says, add semicolon, which is the one thing we did. And there was another error, expected parentheses, found this. So, the one right below -- if we scroll just a tiny bit.
Jason: Yeah. Here. Yeah. So, this is the one that threw me.
Prince: Yes.
Jason: And I didn't realize there was another one above it. Yeah, I understand. So, if I miss it, and that's probably a fault of me not looking at like the full error output. Because it's fairly obvious when you're looking at the full output. But I was scrolling up and I just stopped at the first error. So, this is very, very helpful. Hey, you should fix that.
Prince: And it's good. Like I said, it kind of knows that the -- the errors usually are best read from top to bottom. Chris mentions that in the chat. And that is very true. It's usually because of it being like compiled is like one of those things where it's like, ah, I can find the error first and throw more errors if I see them. It's hard to know which one do I need to be falling for. One of the error that came up, the expected parentheses because of default return type. That error is saying the main function is not supposed to return anything beyond in empty parentheses. What's happening is you were returning the debug statement.
Jason: Oh!
Prince: It's basically like, I don't know what to do. And that's cascaded by returning person. And that goes to the part of there is somewhat -- it's not -- I don't want to say this. It's like... it's kind of like an implicit return, but it's not actually true. (Laughter)
Jason: Okay. Cool. All right. So, yeah, yeah, that makes sense. That makes a ton of sense. I -- man. I'm having fun. This is great.
Prince: Love it!
Jason: So, let's take a look here. And so, now I've -- I've put this together where, you know, we've defined -- we want a string and then we have our unsigned 8-bit integer for an age. And as these expanded, like if we had complex objects where, you know, you had a pet and then the pet had some sub things like type of pet or age of pet or dietary restrictions for pet, you know, because we have bogey pets out here. Prince, that joke was funnier than you acted.
Prince: I wasn't listening, I'm sorry.
Jason: God, I'm like wasting my best material on you over here. (Laughter) So, as we get -- as we get into more complex object types, I can see this being complicated. Because like would I be able to do something like let's saw I wanted another tuple in here. Like I want an -- my favorite color is an rgb value. And so, I would do like let's say that my favorite color is just bright red. What happens if I try to do that? It does work.
Prince: Yep.
Jason: Okay. This is getting hard to read, those.
Prince: Yes this is where structs can save us. One of the things with this is we have this strict structure with what we're trying to do. We want a name, we want an age, and a set of colors. If we were to look at this, it doesn't look like anything, it's not semantically meaningful. Let's do a struct.
Jason: Is there a standard for above or below?
Prince: It is more convention than must dos.
Jason: Okay.
Prince: And even then, I think everyone follows different things as they do.
Jason: Okay. Where do you like to put it?
Prince: I usually like to do mine at the top. I like to put things easier to read at the top.
Jason: And that's not it.
Prince: No, that --
Jason: That is a keyword?
Prince: Yeah.
Jason: I'm ready.
Prince: We have to give it a name. What would you want to name the struct?
Jason: Let's call this a person.
Prince: Perfect. Works beautifully already. The struct, we give it a name and you knew to make it as a capital nature. That's out of everything that we write. And put the curly braces and then put what are the fields inside of that instruct. Yep. You're already getting into the swing of it.
Jason: Is it like this? Like --
Prince: Very close. You want them actually to be semicolon, sorry. What is the word? Commas.
Jason: Commas? Okay. And then color would be... okay. And then does the last one also have a comma? Or do I terminate it?
Prince: I think it's actually fine to have both.
Jason: Okay. So, like this?
Prince: Without the semicolon
Jason: Without. Okay.
Prince: Then what we can do is make a few struct. Let's go back down and let's try to take all of those values and put them inside of our struct. Say the struct instance itself. Say person and then not with parentheses. We'll actually use the curly brackets. Which this is a part where it gets a little bit more confusing. And we need to connect them to the name. Similar to like an object inside of JavaScript.
Jason: Oh, I get it. Okay. So, I can do like name, age. Color. Right?
Prince: Yep.
Jason: And then would I do like this?
Prince: Yep. And then you want the semicolon at the end of this one.
Jason: Okay. All right. Okay. Let's try it. Let's see what happens. So, we're gonna pull this up here so we can see it. And then I'm gonna run this. Oh, no!
Prince: And I don't think it will work because if you --
Jason: Lifetime parameter. Lifetime?
Prince: Because I need the lifetime? So, bear with me. Mmm...
Jason: Chris says use string.
Prince: Yes. So, we're going to -- this is where things get a little wonky. I'm going to show you both things.
Jason: Like this? Or -- wait.
Prince: We'll keep this and then we'll put a static in front of it. This is a life time. And we won't talk about that. I will not be able to explain that to you in 30 more minutes.
Jason: Okay.
Prince: But the kind of synopsis is that we basically have to describe how long a specific thing has to stay available for us. And this is just doing that for us.
Jason: Okay.
Prince: In like the shortest version. It's not the most accurate. But the synopsis of it is is that's what it's doing here.
Jason: Now it's doing like -- cannot be formatted using this thing.
Prince: Yes. And this is where sometimes it's easier to use the pretty print version of it.
Jason: And that's when you do this.
Prince: And it kind of told us there before.
Jason: Do this?
Prince: Yep. Let's try it. Should work.
Jason: Cannot be formatted using this one. So, if I leave that out.
Prince: But didn't that not work before?
Jason: No!
Prince: How are you going to give us recommendations to do something and it's not gonna do that thing?
Jason: Doesn't implement standard format display.
Prince: Yes. And so, this -- is one of those things that someone --
Jason: May be able to use -- oh, what about this one?
Prince: What is -- I cannot see that one. I don't even know what that is. Yeah. So, what we're going to do instead, it's going to be easier this way. Is let's go back to the top. And I'm gonna do this one for you. We're gonna derive, debug. And let's do that question mark version again.
Jason: Okay. Before I run this, do you want to tell me what this does?
Prince: Yes. So, this particular thing is -- is like -- one, is an attribute. It creates something for us. It does -- and you can kind of think of it as similar to meta programming. It generates code for you.
Jason: Okay.
Prince: It's going to do a bunch of stuff for you. Some languages call them macro. But this one is called an attribute.
Jason: It's applying it to the struct?
Prince: Yes.
Jason: Got it. This would kind of be like as a JavaScript parallel, when you have like a -- like the classes where you would like do the@ thing and attach something to the class?
Prince: I think those are called decorators?
Jason: Decorators, is this a decorator?
Prince: I get kind of nervous calling them things that they are in other places. They're like a decorator, they're kind of, but they do so much. They do so much. It's a loaded term, is yes the answer? I think of them like that. If I put them together. And like Chris says, it's a Babel plugin built into the language.
Jason: Okay.
Prince: You can do a lot more than just what decorators allow you to do.
Jason: Here is debugs very nicely. And does this mean then if I were to switch this back out to debug because we derived it from debug that it should theoretically do what we want? It does. Very nice.
Prince: You can see it gives us the name and the keys attached to the value.
Jason: This is meaningful. This is useful as opposed to having to remember that we defined a person in these very loose terms of like, that's what a person looks like. And, you know, that wouldn't help us.
Prince: Exactly.
Jason: This is helpful. Okay.
Prince: And give us more structure. And that's what they come with that name for.
Jason: Yeah, yeah. This is cool. I like this a lot. All right. So, let's maybe -- can I nest these? Like what if I -- okay. Let me try one on my own and you can tell me if this is way out of -- way out of dodge here. So, I'm going to derive debug. And then I'm gonna --
Prince: What is that called?
Jason: The struct. What was it called? It was called... deriver.
Prince: No, it's an attribute. That's totally fine.
Jason: Attribute. And then I'm gonna define rgb. Not Ruth Bader Ginsburg. And then we're gonna set this to have a red -- oh, wait. If I wanted to be just like a straight... can I do a struct that's like... let me say what I'm trying to do and then you can tell me if this is a thing that actually makes sense. I want this to be, like, a type like that. So, that we could -- we could reuse this later if I wanted to like define a color outside. So, if I still wanted to be just a tuple like this... do I have to give it a name? Or can I make it like top level like this?
Prince: So, you would want to give it a name.
Jason: Okay.
Prince: But let -- maybe one of the ways that we can do this, and this can be a fun experiment for us. This is something I didn't anticipate. But let's maybe change, instead of calling it rgb, let's make the struct called color and then the name is rgb. So, like inside of the field, that is rgb. Which begs the question of like, how do we all do these things? And perfect. And let's try making a color, storing that into a variable and then passing that into our person. And then making it all happen. It's not going to work now. Everything's still broken. But you see the error here.
Jason: I do see the error. Which means, then, I need to do, like, a color... okay. That got us there. So, this is -- this is fine. But it's nested. I mean, that's okay. We can -- we can leave it there.
Prince: So, we can do other things as well.
Jason: Okay.
Prince: So, one of the things that kind of like was suggested as well is what is known as a tuple struct.
Jason: Okay.
Prince: So, like it's not like the syntax you had before. But it's still very valid. I just want to throw you through all the loops because that's me. I'm a teacher at heart. So, instead of having the field anytime, like rgb, we can be like, struct color. And then just pass -- just give the tuple as-is.
Jason: So, no curlies?
Prince: Yes.
Jason: Okay. Like that.
Prince: Yes.
Jason: And then down here...
Prince: And I think we will want a semicolon in front of that one. Sorry, the struct above, yes.
Jason: Okay.
Prince: Let's try that out this time.
Jason: Do I need to define it as a color? Or can I just throw it? I'm gonna try it. Watch it explode.
Prince: You probably still need to give it the color name.
Jason: Expected struct color got a tuple. So, if I send in a color... all right. I'm into it. I like it. Okay. This is -- this is all starting to click for me. This makes sense. All right. So, we can do these kind of like strict definitions where if we've got a color that we know is a list of three 8-bit integers, then we can set that here. And then I could also use that out here as like a color. Right? Theoretically. No. Expected struct color found a tuple. Oh, so, I would have to --
Prince: Yes. And so, this is where you have to like pay attention to all of those types and all the things. And the question was asked, is struct like a class? In the sense, yes. A struct is similar to a class. It can do things like tell you kind of like the type, give you kind of structure to that thing. It doesn't do classes specifically. But this is kind of the touchpoint to where, like, object-oriented program comes to Rust. Rust kind of does both object-oriented-ish. Because those structs can also have methods on them. And then what do you call it? I lost my train of thought. Oh. Functional. There we go. And it also has functional capabilities. So, like map, filter, those also exist.
Jason: Oh, okay. Can we write some functions or did you have something else that you wanted to go through before that?
Prince: Hm.
Jason: We got about 20 minutes left, just time checking.
Prince: Let's try to make a method for our person struct.
Jason: Okay.
Prince: So, making a method for a struct actually means that we're going to do some implementations. So, we're not gonna write anything inside of the struct.
Jason: Okay.
Prince: But what we're going to do is we're gonna write in a new block. And outside -- yeah. We're gonna say, imple, i-m-p-l-e, that stands for implementation. We're going to implement for a thing. We have to describe that thing that we're going to implement for. And we'll say the person.
Jason: Like that?
Prince: Exactly. And then we'll have you are a curly braces afterwards. And here we will be able to describe any functions that we want to have for our person.
Jason: Okay.
Prince: So, if you wanted to, yeah.
Jason: So, we can do something like this.
Prince: Yes. And we still have to describe it as a function, so, we will use the fn keyword in front.
Jason: Okay. And let's go without any of that, then we could probably have it like... all right. I'm gonna -- let's see. That's not gonna work, is it?
Prince: No.
Jason: Okay.
Prince: I'll tell you how to make it work, though. Let's do it this way. So, we're trying to make it a method that is related to our person. So, we actually need to tell our method here, you have the value of that person. So, as an argument to say hello, we're going to use ampersand self. And that ampersand, that thing, once again, it's talking about a sense of ownership. We're not owning self. We're just taking advantage -- we're borrowing the value that self is. That's why we want to have that there.
Jason: Okay.
Prince: Now we can use it as the name itself, we can do self.name.
Jason: And then if I want to try it out, I would say person.sayHello. And assuming -- nope. Doesn't like that because value borrowed here after move. Copy traits -- maybe it's just I debugged it?
Prince: That's a great question. Shouldn't happen like that. Let's try it out.
Jason: Okay. It doesn't like some stuff. You think maybe because we used debug as the derivey thingy.
Prince: Yes. And I don't think why the debug did that.
Jason: Should have a snake case name. You have a snake case name.
Prince: Yes, everything inside of Rust is preferably snake-cased. That goes back to one of the biggest concepts inside of Rust which I didn't think we would get to. The concept of borrowing. This is very different than something like C. The values can only have a single owner at a time. Or a variable -- or like things that exist can only have a single owner. And that means like I can -- once I take ownership of a thing, whoever else had ownership does not have that thing anymore. We cannot have two owners at the same time. And this is the reasons why you'll see those ampersands is that we're not saying we have ownership, we're actually saying we're borrowing a value. So, when I have that self there, I'm not taking ownership of self, but I'm rather using the value. Just a nice little hand holding. Can I have this for a little bit?
Jason: Okay.
Prince: And then you give it back to whoever is the owner.
Jason: Got it. But whether we do this, apparently debug is not borrowing. It's like, give me that. It's mine. When we run the debug command?
Prince: In this case, we are talking about a type. So, it's like a generic type. What we are talking about is an actual value with the self. And what somebody had mentioned in the chat is like we're moving person into debug. And this isn't always the case because I think you could do the same thing with print line, it would be fine. But I don't know why debug does it and it specifically must be that it's moving it into this particular macro. Let's use the print line function.
Jason: Does not use the copy plate. I would have to change some stuff. But if I use print line... then it just works.
Prince: Yeah.
Jason: Okay. So, debug does some business.
Prince: Yes.
Jason: And we would need to argue with it a little bit to get it to share. So, debug --
Prince: Yes.
Jason: What we're learning is that print line is friendly and knows -- and like attended preschool. And debug is like the -- that like mean only child who is like, give me that! It's mine!
Prince: Exactly. And I think that's like important to know about Rust. And sometimes you'll see those errors. As you're playing around with functions, as you're playing around with a bunch of different places that you want to put code. The compiler is going to warn you whether it notices that you're doing something that it's not allowed to do. Rust is really intelligent in the sense that it can know ahead of time. It checks all the rules for all the pieces of code to check if that works.
Jason: Yeah. Okay. And so, like, if I want to -- can you just stack stuff? Like I could just be like, also do this? Or does that --
Prince: Yeah. You should be able to do this. I don't know if you typically try to do that.
Jason: See what happens. I do what I want.
Prince: All right. Cool. Cool, cool, cool... cool. Cool.
Jason: All right. What are you going to do? Are you going to cooperate? No. You're not going to cooperate.
Prince: Yeah.
Jason: Does not implement copy. Touching my stuff.
Prince: Yes. And this is an important part about it is the copy -- the things that we're doing, the derives, sometimes need to be able to implement certain features that like describe how am I supposed to do that thing? So, like debug, for instance, it kind of -- it doesn't need everything. Oh, cool, take what you need to. But copy requires a little bit of additional work.
Jason: Okay. Okay. I get it. Well, cool. So, this is -- like, I definitely feel like we're, you know, we're barely scratching the surface here. So, what if we -- if we wanted to do something like instead of saying hello, what if I wanted to return a value and then like use that value somewhere else? So, like instead of having the function print, like could I store the output of this function in a variable out here? Like, you know, something like let... we'll do it the Rust way, I guess. And like have say_hello return a string instead of just printing immediately? Is that a thing in Rust -- am I confusing myself?
Prince: No, no, you're totally fine. So, what that would mean is that we need to return back that string, right? We would need to be able to do this. And this is probably where we would want to do the actual, like, return of the value. But right now we're not doing that.
Jason: Right.
Prince: And so, let us change this to format. Someone actually mentions this in the chat. Is you can use the format. I think that should be fine.
Jason: Is it like fmt or like formats?
Prince: The full name like this.
Jason: Okay and does it still have an exclamation point?
Prince: It does. And we need to specifically return something. Return, and we need to tell the function it's going to return. Before we didn't tell it to return anything, it was doing what it's doing. This time we're actually going to return like a string. So close. So close. You --
Jason: I wanted to believe.
Prince: This is... the things that we wanted too. And I know that we can -- there's like certain ways that we can also enhance this. But I want to walk through all the ways first. So, chat knows, like, hey, we're gonna do this the long way first. But we're gonna do string this type instead.
Jason: Oh, okay. Okay. All right. And this is because if we did it this way, it would be expecting like a set length of value and that is not the case? Or because that wouldn't give up ownership. it would be bar rowing. Wait. I have no idea what I'm talking about.
Prince: This goes back to two different types of strings in here. There's known things and some unknown things. We're going to accept this is the one that knows, hey, I'm dynamic. I can change in size as much as I want to. Whereas the other one's like, cool. I want to stay in this specific size. And that's kind of the difference.
Jason: Got it. Okay. Great. And then is the semicolon in a return statement? Do we leave that? Or take it out? Or...
Prince: Let's start with this one and see what happens.
Jason: Okay. Okay. I'm gonna try things. Okay. It -- it did the thing, I think?
Prince: You did the thing. Awesome. Now what someone else had suggested is to remove the semicolon. We can also remove the return in front. And this is an implicit return.
Jason: Okay and so, this, I imagine, is one of the things that the first time you see it, this is terrible. And then it's the only thing you'll ever use once it clicks in your brain?
Prince: Yes. There are also edge cases where you don't want to do this. You want to be more explicit than it is implicit. I think Chris actually wrote a blog post around this where it's like, I thought I could do this and I cannot. And it's specifically around if conditions. Because there's like a weird syntax when you read it. You try doing everything like this. But then it actually turns out, it bites you in the butt when you do it. So, that's why I suggested starting with this one first.
Jason: Ooo! I have another idea, then. Can I just add another function down here.
Prince: Try it. You should be able to do it just like that.
Jason: Let's see... and then I would get my object. And this is gonna return a string.
Prince: All right.
Jason: Right? And then now what I want food is I want to say, if -- we're just going to start writing code and you're going to tell me that it's wrong. Say, if age is less than 21 because this is a US-based program, then we would want to return -- I'm gonna try some silly things and see if they work.
Prince: I think this is the best part about programming. This is why I let you go on. Because I want you to experiment and feel what it's like. Because that's the only way you get to really learn a language, right? Is like you're not just following the steps from a book. But you're also like experimenting with what you are learning.
Jason: Okay. So, let me walk through where my reasoning is on this.
Prince: Okay.
Jason: This, without a semicolon, is an implicit return. So, what I'm thinking, I don't know if comparisons work in Rust so we're gonna find out.
Prince: they do.
Jason: But if the age is under 21, then I am implicitly returning this string. Otherwise, I'm implicitly returning this string. So, what I expect to happen is that if I... if I do something like this, beverage... what should happen then is that I will do something like this. And then we'll just print out another line here. And I think it's gonna explode, it did.
Prince: It is gonna explode. Let's see if we can read the errors.
Jason: There's so many errors. I did everything wrong. The first one is an obvious one. I just typo'd. I should have done self.age instead of age. Fix that again, run it again.
Prince: And I like your process of just do one thing and then break.
Jason: Yes, that's my favorite way to be wrong in programming is like one step at a time. Because I remember when I was earlier in my career, if something went wrong, I would go and change a bunch of things thinking that I was solving the problem and then I wouldn't know if I had like fixed one problem and created a new one, or if I had actually changed something. Now I'm like, one line. Did it break differently or the same?
Prince: Yes. Definitely. And one thing that Chris mentions as well, and I think I have never thought about it like this. But I actually really love it is the framing around how we treat them really shapes like our how we engage with them. So like, for instance, Chris calls them help texts instead of errors. That's a mindful way of making sure that you're taking the time to understand it. It's not trying to work against you. It's trying to tell you, hey, do the thing.
Jason: Okay. So, now I have issues --
Prince: Yes.
Jason: Where I have mismatched types, expected a string.
Prince: It wanted to receive.
Jason: I apparently sent a variable -- or a fixed length string.
Prince: Hmm hm.
Jason: Try using a conversion method. Okay. That's helpful.
Prince: Oh, that's so cool. I didn't realize you were doing both of them at the same time.
Jason: Hey! It worked. Now let's check if my lodge you can works by changing my age.
Prince: Also an important thing to test. Hello, Hasan, we are talking about Rust.
Jason: Hey! What if it's right on the line? What if I am 21? Okay. 20. Look at that. It is doing exactly what we expected. And I will say, this is -- if that's not an endorsement of Rust, I don't know what it is. Because I have literally no idea how to write Rust and I was just able to reason my way through based on the help text, the error messages, all the things that were happening, that all coached me through refactoring that my best guess code. Which is -- that's pretty awesome. That's really, really nice that it does that for you.
Prince: Yeah. And you've learned a ton of things. For the most part, you've like took everything that I've learned from reading the Rust Programming Language book and like put it together. There's a few more concepts that come along the way. Things that we don't have the time to talk about. But --
Jason: Yeah, we are out of time, unfortunately.
Prince: enums, talk about match operators. Those are similar to -- what do they call them?
Jason: Like switch statements? Oh, like fancy switch statements.
Prince: What are they called?
Jason: Pattern matching?
Prince: That's the one!
Jason: Pattern matching is fancy switch statements, Prince, come on.
Prince: yes.
Jason: It's like a switch with a monocle. It's super-exciting. For those of us who want to take this further. I want to see what I can build with this, I'm going to come up with excuses to build something real. What should we do next?
Prince: I think the thing that I want to kind of leave you with is to figure out like how you learn more things. One of the ways that you can go learn more is let's go to your terminal real quick. And we're gonna type a command in there. Is say, Rustup. And then space, doc, and then space with the flag, book. So, two hyphens and then book.
Jason: Beautiful, oh, and this is local.
Prince: Yes. That's what I wanted to make sure to show. So, Rust out of the box comes up with a bunch of different materials for learning. A bunch of it is the Rust Programming Language book as well as exercises like known as Rustlings. They're all hosted online as well. Go to Rustup doc, once you've installed Rust, you can find all the different materials that are locally available for you. Basically everything is live as well.
Jason: Yeah, this opened in the wrong thingy. But it's over here. Yeah. This is really exciting. So, here is the edition guide, the Rustc book, the cargo book, reference, Rustonomicon. Good naming. You know? I'm a fan of -- oh. And then also, we have to show-- let's pull over here. The Rust bot. We'll just do it right in general. It makes me so happy. But so, this -- yeah, this is a super-fun thing that we can do. And this is -- you built this, right? The Rust bot?
Prince: Yeah, so, Chris, Derrick, myself as well as Will, we have all been working a Discord bot.
Jason: Where would I find this?
Prince: It's at GitHub.com/partycorgi/corgo-bot.
Jason: Okay. And here's a Rust bot in here.
Prince: Yeah, there's two, one is in JavaScript, one is in Rust. We have primarily been spending a lot of our time in Rust. Right now they're a separate set of commands. This is using a lot of the same things that we talked about today. Things that we also didn't get into such as a whole bunch of things. A ton of things in here you haven't seen yet. So, a lot of it you can read, you know, like structs, you know like variables. You know a lot of the things in here. So, that's at least good.
Jason: Yeah. Yeah, yeah. So, this will be a fun thing if anybody wants to go get involved, you can go drop in here. You also have the Discord that you can come join with us if you want to learn more. If you just want to hang out with a bunch of people with a whole lot of different interests and people who just like to learn and share and do stuff, play games, learn stuff, come hang out in the party corgi Discord. That is at partycorgi.com if you want to get more details. Hope to see you in there. Prince, where should people go if they want to learn more about you outside of your Twitter?
Prince: Outside of my Twitter, really only Prince.dev, that's really the only place I live on the Internet.
Jason: And you have all sorts of stuff going on with Rust as well. You're taking notes. You have been doing --
Prince: That's a good point. I do live streams every Tuesday at 6:30 EST or GMT -- what is it now?
Jason: Are you maxcellw on Twitch?
Prince: Yes, maxcellw on Twitch.
Jason: Go follow for more Rust streams. And I think that is all the time we have today. One more shoutout to White Coat Captioning. Amanda, thank you for the captioning today. That's made possible if I Netlify, Fauna, Sanity, Auth0. We have so much stuff coming up. Today, next week is Kelly Vaughn, Shopify stuff. Then Emma Bostian is coming on and we're going to mercilessly ridicule her about liking Taco Bell. That's going to be a fun episode. It's going to be fun. We're building a thing. I Hope it's going to be funny. Please tune in. And then Ben Ilegbodu is going to come up and teach us about TypeScript. If you haven't seen Ben, he is a fantastically like engaging and funny speaker and teacher. Eve, Eve is like one of my favorite people in the world. Teaching about GraphQL. And Shelby Spees about observability. I'm so excited to this one. This is like today, Prince, I have no idea what I'm going into. I'm going to be asking all the beginner questions.
Prince: That's okay.
Jason: It's good stuff all the way down. Please hang out with us. Prince,fully last words before we wrap up?
Prince: I think my last set of words for you all is explore something that makes you excited. Explore something that's unknown to you. Take it with the beginner mindset as you do because it's super-important. Otherwise you're not going to let yourself explore and be excited for all the things you can find.
Jason: All right. that, that's all we have today. We're going to raid Chris on Code. Prince, thank you for hanging out with us. We will see you next time.
Prince: Bye, y'all.