657

August 23rd, 2023 × #Rust#JavaScript#Web Development

Rust for JS Devs — Part 2

In part 2 of Rust for JavaScript developers, Scott and Wes dive deeper into Rust's type system, talking about integer types, strings, string slices, arrays, tuples, and other collection types. They also cover defining structs, adding methods, and other fundamentals.

or
Topic 0 00:00

Transcript

Wes Bos

the podcast with the tastiest development treats. We've got part 2 to One of the most well received podcasts we've done in quite a while, and that is Rust for JS Devs. People really liked, rid. Scott kinda went through some of the very basics of Rust and explained what they are in relation to a JavaScript developer who knows what those things are. Rid. Now what we're gonna do is to dive into part 2, which is a little bit more of the types and integers and Different types of numbers, strings, the as keyword, you name it. Hello, Scott. How are you doing today? Oh, I'm doing Hey, man. We've been, having so many audio difficulties.

Topic 1 00:13

Rust for JS Devs Part 2

Scott Tolinski

I it's the this is for all you We have.

Topic 2 00:53

Audio interface issues in Chromium

Scott Tolinski

We have. I have. Sorry.

Scott Tolinski

I have been having so many audio issues, and it's for all you web heads out there, this is Like a a really interesting bug. And and actually, West, just like moments ago, I found somebody on Reddit with the exact same bug with a different audio interface.

Scott Tolinski

So, they did not find any solutions, unfortunately. But did they say all of a sudden, their audio inputs, Like, for their interface has stopped showing up on all Chromium based browsers.

Scott Tolinski

So for me, All of my audio inputs that I have have, you know, it's like some of the default ones work just fine, but anything that's related to my audio interface does not show up In Chrome and all Chromium based browsers, including an older version of Chromium that I downloaded directly, like an older like, a 2020 version of Chromium.

Scott Tolinski

So Yeah. I am very confused As to why that might be, it works in Firefox. It worked in Safari.

Scott Tolinski

No no Chromium based rather. So If you're out there and this this type of bug has resonated with you and you have some insight here, hit us up because I'm I'm trying to figure this one out. All of a sudden, it doesn't work. This person was having a a same issue with a a UAD Apollo interface.

Scott Tolinski

So

Wes Bos

that's the one you you have a u UAD device, so It's interesting that mine is not working. It's very it's definitely something like system level. You know? Like, we've we've sort of pinpointed it to it's not the website. It's not rid Just it's all Chromium based browsers. And, like, the only time I ever have issues with interfaces not showing up is when I have running Zoom. Rid. And Zoom does something to jack the interfaces and lock them.

Topic 3 02:18

Let keyword for immutable variables

Wes Bos

And specifically, Google Hangouts does a good job at rid. Detecting that and saying, hey. You're not running anything else, are you? And if you quit Zoom, it will will tell you. But it's it's bizarre, so, hopefully, I can get it pinpointed. Yeah. Rid. It it's so weird that it's only Chromium based browsers in literally every Chromium based browser.

Topic 4 02:44

Mut keyword for mutable variables

Scott Tolinski

New profile, old profile, Granted all the permissions, like, you could try everything. So I'm, like, 1 step away from reformatting my computer over this, which rid. Obviously, it would be awful, awful process. Well, Wes, how are you doing today? I'm doing pretty good. Just up at the cottage,

Wes Bos

enjoying all the summer. And I'm working on a deck right now, which is kind of fun. I've never built a deck before, so it's flexing my carpentry skills. Yeah. I've built,

Topic 5 03:21

Const variables in Rust

Scott Tolinski

I I built, 1 deck with my dad. My dad has designed a couple of decks. He's like, rid. Maybe even built, like, 3 decks or something. He he built my all of my family's decks, which are auction actually, like, really Intense decks. So when we we had our house in Michigan, I asked him to help me, and he designed our deck, and then we built it together. And then Deck building, it's an intense process, but it's super rewarding. Yeah.

Wes Bos

It's it's wild. It's you look now that I, like when it got into it a little bit, I, like, start rid. Looking at every deck I see and every dock that I see, and I quickly realized that most people are just slapping lumber together, which is rid. Sometimes fine for, like, like, ground decks, but, like, the high ones, you don't want to mess with it. You have to, like, understand, like, how how weight is transferred down to the ground. And It's crazy. Some some of these decks you see is just people are just bolt and stuff together

Scott Tolinski

and hoping for the best. Totally. Yeah. My dad was an engineer, so his His whole thing was better to over engineer it than to under engineer it, and, they were always, like, super sturdy. So syntax is presented by rid. [email protected].

Scott Tolinski

And let's say you're getting rusty and you need to debug your Rust apps.

Topic 6 04:41

Debugging Rust apps with Sentry

Scott Tolinski

Well, you can actually do Rust air Tracking and performance monitoring with Sentry. So just like Sentry is awesome for all of the tools that we know and love inside of our JavaScripts and our, everything like that. It works well in so many other things, including, Unity if you're doing game dev or Unreal Engine if you're doing game dev. But it it can debug serverless. It can debug Ruby, Pearl, Python, PHP.

Scott Tolinski

It can do c plus plus rid. Kotlin. No. Kotlin.

Scott Tolinski

Oh, yeah. It can do everything. Honestly, it's not a bad like,

Wes Bos

You know, there's, like, hello world in every language. It's not a bad thing to look at how to set up Sentry in every language because it gives you a nice little end product. In every language. It's yeah. It's kind of it's kind of a nice little hello world. Yeah. It's the same thing in every language. It's beautiful.

Scott Tolinski

There's Cocoa. You can do any of the Apple stuff. Right? You're doing the macOS. What what are you gonna do? Like, a Vision OS app? Maybe you're gonna put 10th century on that bad boy. And that way, you can track error's performance.

Scott Tolinski

You can do so much with this tool. You can dive in and find out exactly what's going wrong, especially if you're just learning Rust. You're bound rid have some errors and mistakes, so check it out at century.i0.

Scott Tolinski

Use the coupon code at tasty treat, all lowercase and all one word, and you will get 2 months for rid. Free. Alright. Let's get Rusty. Alright. Let's get on into

Wes Bos

Rust for JS devs part 2. If you haven't listened to the first one, rid. Maybe a good idea to go give a quick listen to that, but let's start off with variables

Topic 7 06:11

Signed and unsigned integer types

Scott Tolinski

in Rust. How do they work? Yeah. So So I think the general vibe of this entire episode is we're gonna be talking about the type system. We're gonna be talking about variables. We're gonna Talking about mutability and mutability and some of, like, the weirder things you you see in in Rust occasionally that might make you confused, like the, rid. Weird ampersand before some types and things like that. So we're just gonna we're gonna take our previous episode, which we talked about mainly some, You know, high level differences between TypeScript and Rust and, basically, how Rust has cargo to do Absolutely everything for you. So this episode will be a little bit more more code based, but we're gonna be talking about the type system and variables and those types of things.

Topic 8 07:08

Using the as keyword for type conversions

Scott Tolinski

So in Rust, by default, all variables are immutable, and you typically assign them with let.

Scott Tolinski

Let x equals 5. That means that x is going to be always equal to 5. It is immutable. It cannot be changed.

Scott Tolinski

And this is, like, legit immutable. It's not like JavaScript's const that's, like, kind of just more of a suggestion.

Scott Tolinski

You know?

Wes Bos

Rid. Yeah. It's like JavaScript const is by reference, meaning that when you create a variable and you set it to something like rid Like a number, you can never change that. But if you were to set it to an object or an array, it's never going to change that object or array. But the values inside of those objects array can be modified. So if you do ever want to, like, totally freeze it up, you use The freeze API in JavaScript to to do that. Totally.

Scott Tolinski

So, let x equals 5.

Scott Tolinski

Immutable can't be changed. If you want to define a mutable variable, one that is able to be changed, you have to explicitly state that it's mutable with the mut keyword. So you then say let mute x equals 5. Now x is is now mutable. You can do x, math on x and and change it and whatever you need to do.

Scott Tolinski

There is also, however, const. So you do have let and const, Our good old friends from JavaScript.

Topic 9 08:33

Const variables in Rust

Scott Tolinski

Const, however, is something that needs to be defined at compile time. So these are variables that aren't dynamically set.

Scott Tolinski

So you're not setting this as the result of a function. You're setting this to a single value. Right? So these are actual true constants, so to say. These things are set to be a single value, not the return of a function or anything that's that dynamically. Oh, okay. So, like, the the output

Wes Bos

of sorry. A a const must be explicitly set to a string

Scott Tolinski

of and not a string of get name. Correct? Absolutely correct. Yeah. If you and and that's really the difference between let and const considering they're both Immutable by default.

Topic 10 09:16

Type inference in Rust

Scott Tolinski

Just that const needs to be essentially like a true constant. Uppercase.

Wes Bos

This Generally something I do in JavaScript is when I upper case my const variables, They're generally like that. They are defined at author time.

Wes Bos

They're never going to change, and they're not dynamic. They're not an output from a value. So That might be something like a const base name, and that's like the base of a URL before I I make a fetch request or rid. Const per page is going to be I guess, per page might change depending on the user. But if it's something that is just a value that I set at the top of my file And go in. I always use uppercase for that. And is that something that has to be done in Rust, or is that just a, a stylistic choice made by most Rust rid. Stations.

Scott Tolinski

Oh, nice. Rustations. He namedroppin'.

Scott Tolinski

Yeah. No. It is, that's actually what they call themselves in case you were wondering. They are rustations. Rid Yes. It is not required. It is a convention. So the all caps underscore naming of things for constants is just a convention.

Scott Tolinski

And it's a convention that I follow in in TypeScript as well, and some people don't. Some people do. I pretty much just use it for true Constants like this. Right? Things that are are defined once.

Scott Tolinski

So, yeah, it it's not required to be that way, but that's Typically, how you see it being written in the real world. So let's get into the type system. So, you know, we have types in TypeScript.

Topic 11 10:47

Statically typed and strongly typed

Scott Tolinski

If you're out there and you still haven't used TypeScript, types basically are the way that you say what things are. You know? This is a string. This is a a number. Whatever.

Scott Tolinski

In Rust, however, Rust Rust's type system, if you're coming from the TypeScript world, Feels very familiar. It's very TypeScript as compared to other type systems.

Topic 12 11:11

Compile-time type checking

Scott Tolinski

But Unlike TypeScript and JavaScript in general, it is, statically typed and strongly typed. So Your code won't compile if there are type issues, and there's no way around that. You know? Like, in TypeScript, you can just say, Hey.

Scott Tolinski

How about we just compile anyways right now even though the types aren't passing? Yeah.

Scott Tolinski

So this It's basically a system that says all types are checked at compile time.

Scott Tolinski

And just like TypeScript, the main reason is It reduces the potential of runtime errors. It doesn't eliminate runtime errors, but it reduces the potential of runtime errors.

Topic 13 11:43

Ownership and borrowing

Scott Tolinski

So statically typed. It's also strongly typed as in the type of every value is strictly enforced.

Scott Tolinski

You can't get around it. You have to if the thing is the thing, it must be the thing at all times.

Scott Tolinski

And if you're converting it to another type of thing, you have to have some sort of rid. Conversion to another type of thing.

Scott Tolinski

And just like TypeScript, there's also type inference.

Topic 14 12:19

Type inference in Rust

Scott Tolinski

And Type inference is the ability for the type system to guess or not guess, but know what your type is based on its usage.

Scott Tolinski

So just like TypeScript, you don't have to explicitly type everything all the time because you can infer types Automatically. Yeah. The type inference

Wes Bos

is super powerful in in all languages. I was looking at some code I wrote this morning, And it was probably 20 lines of a fetch.

Wes Bos

And I literally typed one thing and the rest of it was just inferred.

Wes Bos

Rid and that's that's really nice to have because you don't have to go. Like, that's the sort of the downside people think to TypeScript a lot of times. They're like, oh, I don't feel like having to add all this rid of stuff to it.

Scott Tolinski

And especially if your libraries are beautifully typed, a lot of it is just inferred. Yeah. Totally. So it it basically like you say, it allows for Nicely readable and concise code without, you know, sacrificing your types.

Scott Tolinski

Also, there's this concept of ownership and borrowing, which does Come into play in types a little bit where you are going to be, owning the the variable itself or borrowing it. We'll talk quite a bit more about that and how that relates to the type system, but it's basically one of these things you don't have to think about in JavaScript ever is memory management.

Topic 15 13:38

Memory management in Rust

Scott Tolinski

And here, because we don't have a garbage collector, you do have to worry about and think about memory management. And, The type system in Rust, it will, yell at you. It will tell you a little bit of what you need to do to figure things out. It's one of those things that Especially when you're coming from JavaScript world, it can feel like you don't know what's going on at first, but it's It's also one of those things that the more you you work with it, the the easier and easier it becomes and the more you understand. So we'll definitely talk quite bit more about ownership because that's a big concept in in Rust.

Topic 16 14:11

Pattern matching with Rust types

Scott Tolinski

Likewise, the type system allows for complex pattern matching.

Scott Tolinski

This allows us to do things like really advanced, almost like switch statement type control flows to be able to easily pattern match To, direct our responses and things like that. It also has the, concept of generics. And the way that generics Work in Rust is is really the exact same as they do in TypeScript. So if generics confuse you in TypeScript, they're gonna confuse you in Rust. They are the same, but it's really just a way of saying, Hey. I don't I don't necessarily, this thing might be a string coming into this function. It might be a number coming in this function, but We can pass in that type when we use it and have that type being used directly as the type itself. It's a generic type. Yeah.

Wes Bos

The best way I've ever heard it described or sorry. I don't want to pump my own horse, but I always call them type arguments, rid meaning that like like a function can have an argument.

Topic 17 14:59

Generics in Rust

Wes Bos

You you don't you don't make your functions only do one thing. If you want them to be dynamic, Pass in the piece that needs to be dynamic. Right? Same thing with type arguments is if you don't know if it's going to be a string, a number, or a sandwich type, You pass that in when you go ahead and use that. I do love the, horn tooting. I I did once hear a really good podcast about

Scott Tolinski

Rust by this guy, Scott Talinski. It was

Wes Bos

Unreal. Unreal. Amazing.

Scott Tolinski

And there's also Something called zero cost abstractions, which because of the the way the types system works in Rust, there's compile time optimizations to make abstractions Cost nothing.

Scott Tolinski

So let's get into the actual types themselves. These are the things that are interesting in in JavaScript and TypeScript.

Topic 18 15:53

Zero-cost abstractions

Scott Tolinski

You know what types of types we have, Wes. Can you name the types in JavaScript?

Wes Bos

String, number, boolean, object, Undefined, null,

Topic 19 16:03

Basic types in JavaScript

Scott Tolinski

and symbol? Yeah. I feel like that I feel like that's right. Right? I thought there were 6, but maybe there's not. I mean, there's more than that. Well, 5 I I said 7, didn't I? There's Symbol is the new one. Yeah. Cymbal's the one that people always forget. Okay. So the types inside of Rust are very similar. You have a bool, which is a boolean.

Scott Tolinski

You have a, char, which is a unicode scalar value. You have Signed and unsigned integers. Now this is where it gets a little bit more interesting than in JavaScript. Right? In JavaScript, we just have number. Right? But here we have I 8, I 16, I 32, I 64, and same with u 8, u 16. And and these things are basically The length of your integer, you could think of it as like really small numbers just need an 8 bit integer. Larger numbers need a larger bit integer, rid.

Scott Tolinski

And that's really just it. And so you're able to essentially define the size of the container of the number In the type, so that way you're not using a larger container than the type of number you're ever going to be able to need. Right? You don't need a 64 bit Yeah. Integer if you're only going to be having 1 through 5. You know? Like like, people might be saying like, that's annoying. Like, I don't I don't Don't box me in. I don't know what my variable my integers are going to be. You kind of should.

Topic 20 17:09

Signed vs unsigned integers

Wes Bos

But I guess the the benefit of that, I guess, is that it's rid That's why Rust is fast. Right? Because it's in JavaScript, there is a max integer, and it is. You can find it out if you go into your dev tools and say, capital n number dot max max_safe underscore integer.

Wes Bos

And that is how many characters long is that? Let me see.

Wes Bos

Sixteen characters long. That makes sense. Right? So yeah. It's like 9 quadrillion.

Topic 21 18:15

Integer sizes and performance

Wes Bos

Basically, it goes super high. And every time you make a number, I'm gonna count from 1 to 2.

Wes Bos

JavaScript is allocating the memory for you to possibly count up to that max safe integer. Right? And that's unnecessary in a lot of cases. So By sort of knowing ahead of time, you get a lot of performance benefits. Right? Absolutely correct. And

Scott Tolinski

and for those of you who are like, well, now I gotta worry about all these different ones, Well, think about it like this. I I would say most of the time, the numbers that you're using are going to fall into one of the, smaller container sizes where you have, like an 8 bit integer is basically anywhere from 0 to 127.

Wes Bos

Oh, I thought it was 255.

Topic 22 19:04

Unsigned integer range

Scott Tolinski

It is 127.

Scott Tolinski

So well, okay. I I'm sorry. That is in a signed.

Scott Tolinski

So you you are you are correct.

Scott Tolinski

And this is the whole bit of signed verse unsigned, which we'll talk about because a signed integer Goes from a negative to a positive.

Scott Tolinski

So a signed integer could go from negative 127 to 127, Where an unsigned integer will go from 0 to 255.

Scott Tolinski

Does that make sense? What is this?

Wes Bos

Rid. Yeah.

Wes Bos

But why and what is that for? Why does that exist?

Scott Tolinski

Yes. Here's why that exists. So we have signed and unsigned in. It's so funny. Every time I see the word signed, I always think about, like, a certificate. Right? Like, this is Like a SSL certificate or something. But that's not what this is. This is a like, you could think of it as a straight up number sign. Is it a plus or a minus? Right? So a signed integer includes negative numbers as in it can have a negative sign in front of it. So that's why the range is smaller because the range is negative 127 to 127, wherein Signed means there's no concept of negative, and it's just 0 is the lowest you can go. Oh,

Wes Bos

that makes so much sense. So half of it is used for the sign. Half of it is used for the sign.

Topic 23 20:27

Signed integer ranges

Scott Tolinski

So if you're getting into negative territories, Signed integers, 8 bit is 127 to negative 127.

Scott Tolinski

Sixteen bit is negative rid 32,767 to positive 32,767.

Scott Tolinski

So if you're if you're looking at the thing like probably enough. Yeah. What do I need here? It's it ends up being pretty obvious because the only time you ever need a 64 bit integer is if you're getting into Truly massive numbers considering a 32 bit integer is, what, like, 2,000,000,000,000.

Scott Tolinski

So, Typically, you'll probably unless you're doing some really high number calculations, you're probably gonna be using an 8 bit or a 16 bit integer most of the time. And then if you need negatives, it's, signed. If it's unsigned, you get that much more. You get up to, like, what, 64,000 or something. 65,000.

Wes Bos

That's cool. A lot of this seems rid Annoying to us as JavaScript developers, but if you can give it a little bit more foresight, you do get those perf wins. Like, I had the same issue with the rid The Syntax site, we are building with Prisma, and we're using the MySQL adapter. Right? Yes. Rid And if you wanna do something as simple as store an array of strings, it doesn't work. Yeah. It doesn't work. Yeah. Like, you can't rid. Store an array of strings in MySQL.

Topic 24 21:33

Choosing integer sizes

Wes Bos

You have to create another table for those things and have a relationship. And rid. I'm sitting there being like, this is such a pain in the butt. Oh, yeah. If I just want a store, Wes comma Scott, and I Wes and Scott will be 2 different entries in a totally different table, and you end up with so many tables. But the flip the like, the benefit to that is that It's going to be fast as hell. Right? And you're sort of doing the work up upfront. Yeah. It is very wild.

Scott Tolinski

But, again, yeah, it's one of those things that we take for granted. And especially when you first get into this, you're like, that's annoying. But it makes so much sense. And, ultimately, when you're writing your code, You do know, hey. This thing I'm I'm tracking days days of the year here. It's it's obviously going to be A a u eight, right, because there's no negative values.

Scott Tolinski

There's less than a 100 and or 250, 255, whatever.

Scott Tolinski

So it ends up being pretty obvious when you're writing your code. It just seems like a lot to know ahead of time when you're thinking about it coming from TypeScript.

Scott Tolinski

Now there's also floating point numbers. There's f 32, a 32 bit floating point, and an f 64, 64 bit floating point. Those are floats Just like in JavaScript, you are decimal based things.

Topic 25 23:08

Floating point types

Scott Tolinski

We also have strings which are dynamically sized strings And that's a capital s string. K. So you'll you'll see string as the basic definition of a string everywhere in your code. However, There's also this weird ampersand STR type.

Scott Tolinski

And, this gets into the lovely world of borrowing, but you could think of the, STR is what's called a slice of a string.

Topic 26 23:37

String types

Scott Tolinski

And the ampersand oftentimes means that it's a reference.

Scott Tolinski

But an ampersand STR is a slice of a string. So you have an existing string. You take a slice of it. You're gonna be using it at STR. You need to take that string to borrow it to maybe grab a subset of that string, whatever. It's an ampersand s t r. That's what it is. Oh, and,

Wes Bos

rid Like like, let's do we have some more examples of what that might be? Like, let's say we had the transcript from this podcast, which was, rid Like, massive. Right? And I wanted to just show stick, like, the first 2 minutes into another string. Is that where I might use a Slice string? Correct.

Scott Tolinski

Yes.

Scott Tolinski

And and we'll even talk a little bit more about that in a bit when I I have a section called what's up with at Or ampersand STR. So Okay. There's also an array type which is a fixed size contiguous sequence of elements of the same type. Okay? There's a slice, a dynamically sized view into a contiguous sequence of events or events.

Topic 27 24:50

Slice references with &str

Scott Tolinski

Contiguous? Yeah.

Wes Bos

Contiguous sharing a common border. Touching. Touching.

Wes Bos

Next to each other or connecting.

Scott Tolinski

Yep. That's nice. There's a a tuple, which is a fixed size ordered collection of elements possibly of different types.

Topic 28 25:07

Arrays, tuples and vectors

Scott Tolinski

Rid. So

Wes Bos

2 bold concept that we've kind of seen around, there's a proposal to add 2ples to JavaScript in the same sort of way. Yeah. That's one of those, like, rid. Gary wants in JavaScript where you hear the word, you know, like, oh my gosh. What the heck is a tuple? But at the end of the day, it's just an array rid where you know how many items are in the array and you know the type of each of those items. So if you have like like, A spreadsheet is probably the most common example. If you have an array a a row in a spreadsheet where you have an ID, a name, and a is cool, That would be a number, a string, and a bullion. And, you know, you know ahead of time that those items are going to be in the ray and, you know, the types of them. Yeah. And it's kind of like, I mean, rid It doesn't,

Scott Tolinski

hooks, don't they return a tuple oftentimes

Wes Bos

or, like, the good state? Yeah. Yeah. Hook is a tuple because, like, it always returns to you.

Wes Bos

Rid. And the first thing is going to be a specific type of your state, and the second thing is going to be a function that, accepts A specific type. Yeah. So, yeah, that is, like, a really good example of a 2 pole. Yes. It is.

Scott Tolinski

There's also something called an option, which Represents an optional value that could be some value or none.

Scott Tolinski

There's a result which is the result of an operation, and result rid. Is one of the cooler ones because, it it allows the control flow to be really nice because the result can either be o okay or error. And if it's okay, it it allows us for really easy error checking. And if you come from the world of JavaScript using async await and checking, rid. Error exhaustive states with catch and all that stuff.

Topic 29 26:22

Option and Result types

Wes Bos

The whole result business is gonna feel like a dream to you. Sorry. I'll rid I'll just say I just wrote some notes this morning on how to do that in JavaScript, because I just recorded a video for my TypeScript course.

Wes Bos

And the next show we're recording after this, I'm going to talk about it, how to use it with Fetch. Nice. Yeah. That's awesome. And, we'll even have an example. Rid. Talk more about air handling,

Scott Tolinski

later on in this series. I have a so many notes for this stuff that, what we're gonna do is we're gonna just see how far we can get each episode, and And we're gonna chop it off at that point and pick it up because I could just keep going on this stuff. Yeah. We'll make a playlist for for all these as well. The new site, as well as a Spotify playlist For each of these so you can go and just listen to them all in one go. Yeah, buddy. Yeah. That's a good idea for the site. We have an added playlist. We should get we gotta do that. That's easy. Okay. Alright. Next up is, which are typically what you'd have. It's a a type that represents a value that can be One of several alternatives you could think of it, Enum, as being kind of like an object that has very explicit value values to it.

Topic 30 27:09

Structs, enums and hashmaps

Scott Tolinski

Rid. The values don't change. Right? Then you also have a struct. Okay? Now struct is probably the closest thing to a JavaScript object. Rid. Right? It's a custom data type that groups together multiple fields of different types. You can have methods in there, although they They don't get defined into the struct as you do in in JavaScript, but you can have as many as many things as you want in a struct. It is very object Like inside of JavaScript.

Scott Tolinski

You also have a Vec, which is a dynamically sigh resizable heap allocated vector or array.

Topic 31 28:20

Vectors vs arrays

Scott Tolinski

So something to note here is that an array as defined is a fixed size, right, where a vector is dynamically resizable.

Scott Tolinski

So you could think of it as like an array that what we have in JavaScript where you can add, remove things, whatever. So

Wes Bos

we have array, A fixed size of the same type.

Wes Bos

So I might have 10 numbers.

Wes Bos

We have a tuple, which is could be mixed types And explicitly known the length.

Wes Bos

And you have a VEC or vector where that is a list of things That can be expanded or contracted.

Wes Bos

So you probably would want a vector in Most cases. Is that right? I I personally end up using a vector way more than

Scott Tolinski

the other 2 options.

Wes Bos

But Yeah. I'm not like a a pro Rust app or something. Do you know the length of your array ahead of time? Right. I find myself

Scott Tolinski

In practice using VEC and seeing it being used more option in in in libraries and stuff. So, Yeah. Definitely 1 you'll encounter a lot. And then there's a hash map, which is a collection of key value pairs implemented in a hash table and a hash set, Which is a collection of unique values implemented as a HashSet. So you could think of these as sets and maps in JavaScript.

Wes Bos

And that rid What are the keys of a HashMap? What can the keys be? They can be anything? Can they be any type? Or can they just be strings? Because that's one of the nice things about a map in JavaScript is that the key of the map can be an object. Rid. So if you you like, the key of of your map could be a div. And if you wanted to count how many times that div had been clicked, You don't have to, like, give a unique identifier to that div. You just literally use the div itself as the key.

Topic 32 29:51

Memory overhead of hashmaps

Scott Tolinski

Is that the same in Rust? Yeah. Hash map. The the types you can have for a hash map, you can use any of the numeric types. You can use the string types. You can use, enums rid Or or custom types. You could define a type and have those be the the key of a hash map.

Wes Bos

Beautiful. Yeah. It's it's funny because, like, most Languages have this, right? Even Ruby has a hash, and we only ever had objects in JavaScript. And now that we have maps, I find myself often forgetting to actually use a map, and I just go for an object, but

Scott Tolinski

rid. Maps are great, man. I almost never use a map. Do they give performance, benefits by using a map?

Wes Bos

I don't I don't know if it's performance benefits, but the API for working with a map is really nice because it has has, rid. And you can reference something by its value.

Topic 33 31:19

Benefits of Map in JavaScript

Wes Bos

So again, like, if you wanted to use an object, let's say we had Scott and me, and we were users. Right? And you wanted to, sort the podcast by who started them rid And you by user. Right? Instead of just using, like, a user ID as the key, you could literally use the user object or the user type as the key in the map. Rid. And then you can say, alright.

Wes Bos

If I wanna get all the shows where Scott started it, I would just say shows dot get and pass in Scott. Yeah. According to this, it does say that they do have higher memory overhead and slightly slower performance. But I I I think in the right situations, that's the rid The ticket. Yeah. I guess it depend depends on what you're doing. Right? Like, if it's if you're looping over 10,000,000 things. Like, I I tweeted out about reduce the other day, and a lot of people were like, use a 4 loop. It's much faster.

Wes Bos

Yeah. I'm looking over 3 things, your brother. Like, it doesn't

Scott Tolinski

Those are the type of micro optimizations that drive me nuts. As a YouTuber, the amount of times that people would be like, rid. You should you should be doing this since it's like you're working with 4 items here. It's not gonna make a difference.

Wes Bos

Rid. We're doing 99 bottles of beer on the wall here, you know, like like doing stock market rid. Nanoseconds.

Wes Bos

Serving or anything that needs to be lightning quag. Yeah. And if you're doing if you're literally doing something that is so big, Then maybe you need to

Scott Tolinski

say, like, should this be this be done at a database level? You know? Yeah. Where could this be done? That, Yeah. That is not where I'm doing it in just straight JavaScript.

Scott Tolinski

Okay. So let's talk about, you know, one of the things we talked about was the signed and unsigned thing where we had our signed and unsigned. Yeah.

Scott Tolinski

But you might have been asking yourself, hey, I have an unsigned value and I would like it to be a signed value.

Topic 34 33:10

Signed vs unsigned integers

Scott Tolinski

How does that how do I convert it to be assigned value down the road if I decide all of a sudden that I do want negative values? Well, You get to use the as keyword. So just like in TypeScript, we have the as keyword and the as Keyword is really similar in Rust as it is to TypeScript. However, it can do one thing that the as keyword can't do in TypeScript.

Scott Tolinski

It can actually convert your value, in some cases.

Scott Tolinski

So if you have an unsigned variable and you want That variable to now become a signed variable. You don't use a method or a function or anything like that. You use as.

Scott Tolinski

So you could say my Unsigned variable is now as an I eight instead and it will both do type Casting just like it does in TypeScript, but it actually will convert it to be a signed number as well.

Scott Tolinski

So it does typecasting just like it does inside of TypeScript, but it also does conversion of numeric types. It can convert numeric types. Yeah. It can convert enums, and it can convert Boolean and char types.

Scott Tolinski

So it can't do all type conversions. You can't do, You know, turn a number into a string or something with the as keyword, but you can convert number types from one number type to another using the as keyword. What about if you're writing a function in Rust

Wes Bos

rid.

Wes Bos

And you say, alright. Pass me a number, but I don't care what type of number you are passing in.

Wes Bos

Is there, rid. A, is that something you might do or should you be as rigid as possible with your types? Or is there like Is there, like, a generic any number

Scott Tolinski

that you can pass in? I think at that point, you would use a generic, not like a generic any number Because You would say you would know when you're calling that function what the type of integer you're working with is. Oh, yeah. Right. Oh, you could infer it as well. Yeah. Oh.

Wes Bos

Oh, okay. I just I just asked chat gpt, rid. How do I make I, like, ask her for a function. It's like, here's I thirty two. But I was like, well, what if I don't know if it's an I thirty two? And then it says, use a generic. And it shows an example of passing it. And that's good because, like, one thing I think a lot of people don't know in TypeScript land is that rid. Generics can be inferred by the type being passed in, and you don't have to rid. Say, alright. Well, I'm passing it to I 32, so now I have to type I 32.

Topic 35 35:25

Type inference with generics

Wes Bos

That can be inferred. Yeah. Totally.

Scott Tolinski

And and and in those aspects, it does feel a lot like TypeScript. Ripped. You know? And that's one of the benefits to working in Rust is that while it it feels like TypeScript on hard mode is really what it feels It still feels like TypeScript in a way that many other things don't.

Scott Tolinski

Okay. So Yeah. That's the as keyword.

Topic 36 36:16

The as keyword

Scott Tolinski

Let's talk about this whole string slice business. Like, what's up with the ampersand stuff? So the ampersand stuff, Which ends up getting to be probably enough to fill the rest of this episode with. It it's that in in a big of a topic here is basically Ampersand s t r, as we mentioned, is a string slice. The ampersand symbol is used in conjunction with the s t rid. Type to represent a string slice or what's a reference to another existing string.

Scott Tolinski

So in Rust, You're always either, referencing something or owning something or borrowing something, whatever.

Scott Tolinski

A it's a borrowed reference to a sequence of characters. You're borrowing essentially a group of characters from another string.

Scott Tolinski

And that's important because it means that because you're borrowing a group of strings rid. Or you're borrowing a sequence of characters.

Scott Tolinski

That means you're not able to mutate that sequence because you're just borrowing it. You can't change it. You're just borrowing it. You're saying, I was trying to think of, like, a really good Metaphor for this. And I was trying to think of, like, oh, you when you do, like, a screenshot and you select text and you are able to pull that text, you're just borrowing it from the main text. You're not able to change the main text. You're not able to change the new text. You're just borrowing it.

Wes Bos

Does that make sense? Yeah. I think so. I'm just rid. I'm trying to understand, like like, what might be another example of why you would want rid Ampersand string.

Topic 37 37:57

Use cases for &str

Wes Bos

I I guess, like so your initial string is always oh, I think I think I understand. So if you have a function that takes in, like, a search term and you're You're passing in like a sentence and a word you're looking for.

Wes Bos

That function would accept not straight up strings but rid. Ampersand strings because you're passing in

Scott Tolinski

an existing string or part of an existing string. Is that right? Yeah. That's my understanding of it. And this is one of those ones that you'll fight a little bit when you're first getting started because you're so used to just everything. Oh, I'll just make a new string or I'll pass this thing. It's Type of string or whatever. I'm not borrowing it. I'm not whatever.

Scott Tolinski

And there there's definitely some caveats here because the way we're talking about this is right now as as as it is, like, Hey. We're just as a slice of a string. We're not changing, or we're not everything.

Scott Tolinski

This is basically for efficient memory usage. It's not having to create a new thing. It's just saying, Hey. I'm I'm just temporarily holding on to this thing. And even and then I'm not holding on to it. I'm it's almost like I'm just, like, I'm just taking it for a second, and then you can have it back.

Scott Tolinski

So, therefore, when I Oh, if I borrow if I borrow a video game for you and then I, you know, I can play that video game, whatever, I can't. If I destroy it, you're going to be really angry with me. So I have to give that video game back in perfect

Wes Bos

condition. Oh, I see. I see. That makes sense because, like, Like, maybe another example would be if you have the person that copy pastes everything, you know, and now I have this Text in 2 different places.

Wes Bos

So, like, Rust is not making a brand new string every single time. Use ampersand string. It's just referencing the original one, and you're not copy pasting. You're not taking up memory, and that's the

Scott Tolinski

rid. Benefit. Again again, it's a little bit harder, but you get the major memory and and performance improvements because of it. Alright. I think I understand that now. It's an interesting thing. And, again, not something we ever have to think about too much. But, again, it's here for memory usage. Right? We don't have to create something new. You're just borrowing it. So When do you use the at STR? When you only need to read the string data, when you're working with borrowed data or string literals, When the string won't be modified or when you want to avoid unnecessary memory allocation or copying. Okay? Again, performance based.

Scott Tolinski

When you need a string instead of a string slice is when you need ownership. If the string needs to be mutable or dynamically modified, If you want to concatenate or manipulate strings with dynamic changes, you need a normal string type or you want to take Ownership of the borrowed data and modify it. You need the normal string type. Beautiful. So if it's being changed or whatever, it's an existing or non existing thing. You need the add string type. And this kind of the ampersand kind of is almost always used in this way where you're taking a borrowing of something. Right? So the ampersand sign, when you see it being used in in Rust, it's oftentimes being used to take Or to borrow without taking on our ship.

Topic 38 40:24

When to use &str vs str

Scott Tolinski

It's often immutable.

Scott Tolinski

It doesn't mean it's always Immutable.

Scott Tolinski

It means it's often immutable. Sometimes there's what's called a a mutable reference, which we'll talk a little bit about that. But, rid. You can have a value, which is as let mute value is equal to 42. Right? You're just setting a mutable variable to the number of 42.

Scott Tolinski

And then you want to have a mutable reference of it. You can use this fancy keyword at m u And then the value.

Scott Tolinski

And then that creates essentially a reference of the value that can be changed.

Scott Tolinski

So, that gets into, like, really more intense borrowing and really more intense referencing of stuff. You can also use it for pattern matching or looping. Right? You can say 4 ampersand item in ampersand.

Scott Tolinski

So if you were doing, like, I want to temporarily borrow essentially this data while doing a for in.

Scott Tolinski

I want to Temporarily borrow it like you see in a, like, a front end template. Right? Each thing has thing.

Scott Tolinski

I think that's that's how you do it in a lot of templating. I'm trying to think of, like, a, react way of doing it. But for ampersand item in ampersand fact is basically temporarily borrowing this value to loop over it.

Scott Tolinski

Okay. You can also do it for ownership transfer or function returns, where if you have an ampersand t, you know, your generic t indicates that a function Returns an immutable reference to the type of tea.

Topic 39 42:35

for loops with &str

Scott Tolinski

So if you are out there and you're thinking, oh, yeah. I got this Rust stuff. Right? The the moment you hit Yeah. The ampersand symbol and the whole mutable references part of all of this is the part where you start to feel like, Wait a second. Maybe I don't I don't got this stuff because this is where it really gets Oh, man. It gets annoying.

Scott Tolinski

And it's a type of thing that gets annoying when you're writing the code. Because if you don't know why you're writing and what you're writing, you can almost feel like you're just, like, tossing in ampersands and, Ampersand string or ampersand mute, to try to save your bacon here, essentially. Yeah. I was

Topic 40 43:33

Debugging &str issues

Wes Bos

rid Wondering, I looked it up quickly, is, like, is there a tool that will tell you when you're possibly doing something you shouldn't be doing? Rid and maybe we'll have a whole show on on Rust tooling, but it looks like there's this thing called Rust Clippy, which will sort of yell at you. And That's one thing I really liked when I was learning TypeScript is I had a very aggressive, ESLint on TypeScript ESLint. And then every time I hit something, I would go re Research it, and it says, alright. This is probably not a good idea because x, y, and z. Yeah. And you know what? The error messages in Rust

Scott Tolinski

are really good. So in the language tools, if you're just you know, you get the just like in TypeScript, you get the little red squiggly, and you get the same type of error. But if you're comparing it at the TypeScript rid. Errors, which can often feel like a giant tree of madness. Like, you're looking at the TypeScript error, and you say, oh, boy. I don't know about any of this stuff.

Scott Tolinski

In in Rust, it typically it's it's way easier to read, and it it the English in it's pretty good in terms of, like, actually being able to understand it. But I've also found that if I'm Copying and pasting an error into, say, chat g p t. I can you kinda gotta know what you're doing a little bit. But if you copy and paste the error with your code snippet and you say this code is giving me this error in the chat g b t, 9 times out of 10, it's going to say, Yeah. You're this needs to be a string slice, and you're like, oh, okay. I got it. This needs to be a string slice, and that can get you through some of those humps. Because rid. Really, the pain in learning Rust is when you hit a dead end, your code won't work, and you can stop at that dead end And you can choose to power through it or unlike in TypeScript where you can say, oh, let me just throw an any any on here or make TypeScript group happy and move along, whatever.

Scott Tolinski

You're kinda stuck until you figure out exactly what it wants. Right? That could be a pain. So I I have a whole section here on mutable references, but since those are, like, really kind of a annoying concept, let's talk about a struct before we finish rid this off this episode, and we can handle immutable references on, like, a higher level episode. Okay? Because immutable reference stuff is is really not that much fun, especially when you're first learning. So a struct It's going to feel familiar to those of you who work in JavaScript because it is really the closest thing to a JavaScript object Where you define a struct, and the way it works is you first define struct as a type. You say struct, the type name, per brackets, and then you Say what's inside of the structure the structure.

Scott Tolinski

It has a username that's a string. It has an email that's a string. It has a, is active, and that's a Boolean. Right? And then in JavaScript, typically, what you do is you create the object. Right? And it's you create the object instance. And in Rust, you do that the same way you would By putting something into a variable, let user 1 is equal to, and then you say the user this is actually where it differs a little bit because you have to use that type here. You'd say is equal to Mhmm. And then you have the type name and then brackets.

Topic 41 46:02

Defining structs in Rust

Scott Tolinski

And then from there, you could put the values in just like you normally Would.

Scott Tolinski

Now here comes the fun part. If we wanted to have methods or methods inside of this object. In JavaScript, You just toss that bad boy as a parameter in your object.

Scott Tolinski

But in Rust, you use this imp imple keyword where, like, implement. Rid. Yeah. Simple.

Scott Tolinski

Yeah. And so after you have defined your struct, you the Struct name. So emple user then brackets.

Scott Tolinski

And then you write a function, f n login, and then you have just like a normal function. From there, you can then use, you know, user.one.login just like you would a normal method. So

Wes Bos

In in TypeScript defining are types, a function is just another type. Right? Like, rid It it inputs and outputs values.

Wes Bos

With Rust, you define your struct, your type. Rid And then to be able to add methods onto that type, it's using the impl. So it's something separate that sort of comes after it. So It's kinda like the data and the logic of a struct or a a piece of data are Defined separately. Yeah.

Topic 42 47:31

Adding methods to Rust structs

Scott Tolinski

Yeah. So it's interesting that you have to do it that way, but that's just that's just how you do it. So, Oh, so what are the difference between a struct and an object in JavaScript or beyond, you know, the fact that how you define methods? A struct in Rust has to be a fixed structure that's determined when it's defined. Meaning, you cannot rid. Add or remove fields from a structure dynamically.

Scott Tolinski

So you have to know what the structure of the object is.

Scott Tolinski

You can't add or remove fields from it.

Scott Tolinski

If you wanna have a thing that you can add or remove fields from, that's when you use a HashMap. A HashMap Dynamic key value pair.

Topic 43 48:36

HashMap for dynamic structures

Scott Tolinski

Struct is for, like, very structured data. Awesome. Yeah. So I think that we can leave it off there. Yeah. There's a lot of stuff. Yeah. A lot of variables and who knows what, but I hope you feel a little bit more confident getting into, especially, you know, like the signed and unsigned integer stuff, the string stuff. Those are the things that you use all the time. And if you think about the types of variables that we use inside of JavaScript, it's like objects, strings, numbers, booleans, arrays.

Scott Tolinski

You know, it's it's no different except in Rust. You end up using Vect. You end up using struct, float, unsigned and signed integers, Strings and string slices more than anything, and enums and tuples and stuff. But, you know, the the ones that you use most frequently are are really Kind of the same. Awesome. Let's get into some

Topic 44 49:20

Sick picks

Wes Bos

sick picks.

Wes Bos

Boy, do I have a sick pick for you today.

Wes Bos

Rid this is a phone mount that I've been using in the gym. So a while ago, I sick picked the Halo Lock, ESR Halo Lock for the car that, like, charges it and docks your phone.

Wes Bos

And that's been working great in the car. But in the gym, rid. I also wanted something where I could sort of bring my phone around to the different parts of the gym and be able to mount it.

Wes Bos

So I found this thing called the Or 0, magnetic phone mount holder. So what it is rid Is it obviously magnets to the back of your phone, and and it has a like a ball bearing swivel on it so you can spin it in any direction. But the other side of it has rid a magnet on it. So you can stick it anywhere that is magnetic. Right? So if you have a rack in your gym, You can stick it anywhere on the rack, which I find really helpful if you're trying to, like, watch a video and figure out form and all that.

Wes Bos

Rid. But then it also comes with these little, like, discs that you can put kind of like the WyzeCam. It comes with these little discs.

Wes Bos

Rid So I put, like, a disc on the treadmill. I put a disc on kind of like the stretching area.

Wes Bos

And it's great because you could just bring your phone around rid And slap this little magnet thing on it and then be able to tilt it in any direction. And I haven't mounted anything anywhere in the gym other than this just little sticky 3 M adhesive tape to it. So I was like, this is such a good option because rid. It doesn't I don't have to change my phone case. Yeah. I don't have to change the fact that it's MagSafe charging or any of that. I just have to rid. Stick this thing wherever I want, and I can just grab it and move it. I only have to have 1 for the entire gym, and I can bring it with me wherever I want. That's it. Yeah. I Yeah. Yeah. I I just use the pop socket, and that would be great if that was magnetic. You could just magnet to anything like that. Oh, yeah. You had Do you you still use the pop? Didn't you buy the that, like, charger that does everything?

Scott Tolinski

I did. Yeah.

Scott Tolinski

So the pop socket for me The pop socket comes off. Yeah. It's just a magnet wallet, and then I I magnet on the thing. Yeah. But the next Step for me is there I think there's one that is like a well, it doesn't do charging through your wallet, so that would be a problem. So I'm gonna have to take it off no matter what either way. So I guess rid. I'm I'm fine with this.

Scott Tolinski

My sick pick is going to be a ThermoCell Patio Shield, which is a mosquito thing. So, You know, we never have mosquitoes in Colorado. It's like I since moving here, I had seen, like, 5 mosquitoes total, But this year has been like monsoon crazy. We just been getting rain every single day in an unbelievable may way and we got mosquitoes somehow. It's like Actual mosquitoes here.

Scott Tolinski

So we've been hanging out outside a lot, and we got this little thermostat patio shield, which it's $18 for the thing.

Scott Tolinski

Rid. But you end up having to buy little fuel cells and stuff. So, yeah, you think $18, but it ends up probably being a little bit We're closer to 30 or something after you you get it all all the little pieces for it. And what it is is it's a battery free Little cylinder that you turn and you put a little looks like one of those, breath strips. You put one of the breath rid. In there. Not a real breath strip, but one of the ones they give you. And it just keeps mosquitoes away for, you know, 12 hours or so.

Scott Tolinski

And I gotta say, I get bitten more than anybody I know, and I I you turn this thing on, and I I don't see a single mosquito. You don't have to cover yourself in bug spray.

Scott Tolinski

It works better, I think, than, like, a citronella candle or something like that.

Scott Tolinski

So I found this to be actually very effective, And I was surprised because when you look at it, you're like, I don't know if this thing's gonna work. Might as well just try it for $20, whatever.

Scott Tolinski

But I found it to be very effective at keeping Mosquitoes away, especially if we're, like, hanging out by the fire pit and just, like, you know, nobody wants to be swatting mosquitoes away while while you're hanging out in the fire pit making s'mores or something.

Scott Tolinski

So

Wes Bos

rid There is, there's a hack you can do to refill those.

Wes Bos

Like, there's a bunch of YouTube videos where guys will rid. Use pliers to pull it off. It's the same thing with, like, the Swiffer.

Wes Bos

You know, like, the Swiffer has that, like So you've heard of this thermostat thing? Oh, yeah. Yeah. Yeah. I've every time I see something like this, I'm like, rid. Someone's gotta figure out how to because, like, anytime I see, like, a product that's, like, cheap, but then they, like, screw you on, like, the refills, and they try to, like, set you up with the, We'll mail you 1 every month.

Wes Bos

I'm I'm always like, somebody's probably figured out how to figure like, because it's just butane in there. Right? Like, that stuff's cheap. Rid. So you should be able to figure out that. I've looked into it because, like, we've got we get not so much right now, but at the beginning of summer, there's a lot of mosquitoes, and You have to douse yourself in deep every every night you go to the fire. I don't love that.

Scott Tolinski

I don't know. I'm I'm weird about that kind of stuff. So, yeah, I'll I'll take the, I'll take the thermostat. It works really well. Did you know, like, there's a lot of, like, little things like that where you can buy generic versions of fancy things. Obviously, you know this. But, like, I found recently you can get Mhmm. A pack of 50 Magic Eraser sponges for, like, $14.

Wes Bos

It's true because it's Somebody those magic erasers, somebody just figured out that this, like, mildly abrasive, fobian material is good at cleaning.

Wes Bos

And I love I love stuff like that where somebody figures out, it doesn't need to be. Yeah. Doesn't need to be something crazy fancy. Yeah. Although I did figure out, That, you know, that spray and wash that you picked a while ago, the power wash, Dawn Powerwash? Yeah. Rid. Yep. I I said, oh, it's just, alcohol and Alcohol and Dawn Soap. It's not. It's it's so much better than that. Like, we've I've I'm not one to, like, buy Expensive things like that, but that stuff is awesome. And you can't it's not replicated. It's not just Dawn and and alcohol. It's it's they've rid. They formulated something something else there. It works really well. I like it still. I'm still using it. It's really good.

Wes Bos

Sick. Jim's plugs, rid. Westboss.com/courses.

Wes Bos

Check it on out if you wanna learn a thing or two about JavaScript.

Wes Bos

Alright. That's it for today. Thanks, everybody, for tuning in. We will catch you later. Peace. Peace.

Scott Tolinski

Head on over to syntax.fm for a full archive of all of our shows.

Scott Tolinski

And don't forget to subscribe in your podcast player or drop a review if you like this show.

Share

Play / pause the audio
Minimize / expand the player
Mute / unmute the audio
Seek backward 30 seconds
Seek forward 30 seconds
Increase playback rate
Decrease playback rate
Show / hide this window