Transcript from the "Introduction" Lesson
>> All right, welcome this is the introduction to rust. So we have some instructions on here on the slide, in this repo, that will help you get set up. So this is things like how to install rust, how to find the companion website for this, and also the slides for this workshop.
[00:00:15] We're gonna start with a little introduction before we get into the actual course material. So, this is gonna give you a little bit of background, like what is Rust? Who uses Rust? Why and why not you might want to use Rust? And the workshop structure. So, this is from the rust-lang.org website, this is sort of how Rust self describes.
[00:00:34] It's a language empowering everyone to build reliable and efficient software. So, both of those two words are highlighted, and they're both important as we're gonna see. So rust is a language that compiles to either machine code which would mean like a binary executable. So like a.xc on Windows or just a binary executable on Mac or Linux or to webassembly, which will let you run it in the browser.
[00:00:55] Who uses Rust, all sorts of people, all sorts of organisations. Mozilla is probably the most well known because they were sort of bankrolling rust development for the first like decade or so of its life. They use it in Firefox to power some of their new rendering stuff that's been a lot faster than what they had before.
[00:01:11] Microsoft has started using rust in a big way they are apparently rewriting some of the core like low level windows things in rust. I don't know if that means they're going to rewrite all of windows in rust that seems unlikely but it is powerful enough that you can use it for really low level operating system stuff.
[00:01:26] And Microsoft has joined the rust foundation as like one of the most prominent members. Dropbox has been using rough stuff to improve some of the performance on some of their sinking stuff. There's also sort of lesser known organizations talk for example is using rust to make an embedded systems operating system.
[00:01:44] Debbie makes makes a game engine in Rust Redux is actually a full blown desktop operating system written entirely in Rust. You can find more examples of this on the Rust website. So you can see there's a lot of diversity in how rust is used. It's used by really big companies, really small companies, not organizations that are not companies, all sorts of different examples.
[00:02:02] So what can I build with rust you might be asking yourself. One of the obvious things for an audience in front end masters is web servers. Like you got a front end and usually you need a back end to go with it. You can absolutely build a web server in Rust.
[00:02:14] You can also build command line interfaces. This is something that comes up if you're doing tooling like build tooling stuff to compile your front end. Also, you can use it to build the front end of a native desktop application. Maybe you don't want to use electron, you'd rather use something else that's maybe a little bit higher performance.
[00:02:43] A really powerful example of this is makepad.dev. I recommend bringing this up on your phone because it's an example of really like what how powerful rust can be when it comes to making in browser UI. Makepad is a fully featured IDE that runs like 60 frames per second on your phone, and it's all built with rust.
[00:03:01] So check that out if you have an example of what rust can do in the browser. Also, you might want to use it for just performance intensive libraries that could be in any one of these applications. Like web servers, CLIs, Native apps or even in browser apps, because again, rust is a really, really good at performance.
[00:03:18] And finally, like we saw on the previous slide on operating systems. if you really want to build your own operating system that is, believe it or not something you can do in rust. So why would you choose to use rest? These are some of the things that can do.
[00:03:30] Why would you choose it over some of the other ways you might build those things. I'm gonna give you three reasons speed performance and going real fast. These are really the selling points of Rust. You'll notice the theme here. This is really like in my mind the reason to use rust over any other like back end technology.
[00:03:48] Or any other front end technologies that you might get Is rust unlocks like basically the maximum speed you can possibly get out of your hardware. So historically like languages that give you really great performance. Are actually really old languages like C is still one of the most widely used languages in the world.
[00:04:04] Because it gets you such strong performance. So you came out in 1972, and its whole big deal is that it's basically efficient. C's sort of claim to fame is being known as, quote portable assembly language. In other words, it's about as low level as assembly language. It has a few conveniences, but it gives you like complete control of your hardware.
[00:04:22] So there's really basically no overhead between your C program and the hardware underneath it. C++ from 1985 also one of the most popular high performance languages out there. Still one of the most widely used languages today. And again, the main reason is because it's very efficient. One of C++ his goals was to introduce object oriented programming to C.
[00:04:43] So that you could have those constructs as a convenience for the programmer, while still maintaining the same level of efficiency and performance as C. In other words, having no or as little as possible between you and the end. What the heck Hardware is capable of. And it's not until 2010 that we get something that really can be considered a serious challenger.
[00:05:02] To the the sort of the throne of C and C++ that have been at the top of the heap for so long, which is rust. Rust again very, very efficient, almost nothing between a compiled Rust programme and what the hardware is capable of. But rather than going in the object oriented direction like C++ did because rust is not an object oriented language.
[00:05:21] Rust said, what, we just want to be reliable, efficient and ergonomic and that's kind of the whole goal of rust. I know ergonomic is not one of the words that they use to describe themselves. They just focus on efficient and reliable. But I would say compared to both of these things, not only is rust a lot more reliable, it's also a lot more ergonomic than C or C++.
[00:05:41] Okay, so here's a concrete example of this. Here's an error that you would get in from the Rust compiler, if you had, let's say a typo. It says no field 'emial' on type 'User' because I misspelled email. It says, note: available fields are: 'name' and 'email'. Now contrast this to the same error in C++.
[00:05:58] This is what you get, main.cpp error, no member named, 'emial' in 'User'. And that's about it doesn't really show you the line number or hint like what are some of the available fields, maybe you have a typo there. So this is a pretty big improvement and you can sort of extrapolate this.
[00:06:11] To more complicated areas like type mismatches and things like that. It's just generally a much friendlier experience than you would get from something like C or C++, even modern C and C++ compilers. Now granted, I'm not gonna say that rust is like the most ergonomic language that I've ever used with the nicest in terms of compiler errors, that would be elm.
[00:06:28] So here's what it is when Rust in the same era in elm would be, something like this. The user record does not have an email field. This is usually a typo here are the user fields that are most similar so maybe email should be email. If you've ever used elm before, I teach a course on it right here on Frontend Masters.
[00:06:42] And it's really the most delightful language I've ever used. But again, we're not trying to compare Rust, at least in my mind, to really high level languages like elm that are sort of designed around ergonomics and delightfulness. What we're comparing it to is languages that have that same level of efficiency like C.
[00:06:57] And C++ because that's where rust really shines is when you want that level of performance. And when you're up at that tier of performance, like Rust is just leaps and leaps. And bounds ahead of the competition as far as I'm concerned. Okay, so why use rust? To sum up I would say you can get C and C++ levels of performance.
[00:07:15] But with nice ergonomics, a language server, we're gonna actually see that when we do the exercises. Automatic memory management package manager code for matter. More compiler help with concurrency not something we're gonna get into in this course. But something to know about rust is it's really good with concurrency.
[00:07:31] Also lots of compiler help for really big code bases like you can make changes to a big Rust code base. And still have a lot of confidence that once it compiles, it's probably just going to work again. Okay, so those are some of the reasons to use Rust.
[00:07:45] Let's talk about some of the reasons why you might not choose rust. Number one rust is a big language there's lots to learn. This is an introductory course so we're definitely not going to get into the entire language. By the end of this course you should have a good understanding of like the basic concepts of rust.
[00:07:57] And the for the things that you would need to learn to build like a full featured huge Rust app. They're out there. There's plenty of stuff there. But you should at least not have any trouble learning those things from here. If as long as you've gotten a handle on the things we talked about today, but there is a lot more out there.
[00:08:13] Really, even though this is a full day workshop, this is kind of just the tip of the iceberg of what's there and the entire language. There's just a lot to learn. It has a smaller ecosystem than something like C or C++ there has been, CNC plus plus being written since 1972, in the case of C in 1985.
[00:08:29] since, in the case of C++ there's just way more CNC plus plus code out there in the world. Now granted, rust does have an FFI, which is a foreign function interface, which is a way that you can call C and C plus code C and C++ code from rust.
[00:08:45] But as soon as you do that you start to lose out on some of our safety and reliability guarantees and the economics get worse. So it's something you can do as a fallback, but it's generally something that you want to avoid if at all possible. Much nicer to have everything in Rust which is why you see projects like let's build a whole operating system in Rust, things like that.
[00:09:02] Also I would say that rust has a slower iteration cycle than most languages. Now I'm not saying slower than let's say C++ but I am saying like languages in general. I'm very spoiled as an elm programmer because the Elm compiler is very, very fast. The rust compiler, let's say is not so much.
[00:09:18] But one of the reasons is, is not necessarily the speed of the compiler, just the strictness of the compiler. Rust is very much a language that sort of helps you build reliable software. By telling you when there are problems with it, not by sort of like papering over problems.
[00:09:31] So there's a lot of cases where the rust compiler will give you errors that you might not see in any other language. These are called borrow checker errors and we'll talk about them later on in the course. Especially as a beginner, the satisfying the borrow checker can feel like an experience of fighting the borrow checker.
[00:09:46] Because this is new category of errors that you've never seen before. I definitely remember when I was a rusted beginner encountering this feeling of working through the borrow checker errors. And seeing like, I see that it's telling me there's a problem, but I don't understand what it's trying to communicate to me.
[00:09:59] So over the course of this course, you're gonna see that we are going to build up ways. To talk about memory in order to give you a mental model of what the borrow checker is actually doing. And hopefully by the end of the course, you'll have an understanding of like.
[00:10:12] I see why it's giving me this error because I know what it's trying to do and the problem is that it's trying to prevent. Also, especially when you're doing a full build as opposed to like an incremental build, the rust compiler is just pretty slow. I wish I could say that there was sort of hope that it was going to get faster.
[00:10:27] But after reading some blog posts from some of the people who were closely involved in the development of the rust compiler. Think it's sort of safe to assume that that's not going to get better anytime soon. Maybe not ever, it might just be a language that always has a slow compiler.
[00:10:40] I think that might just be a downside of the language. But nevertheless, it's got plenty of upsides to compensate for that. In particular tests can take a while to build, like integration tests and things like that. They just take longer to build than I would like. And I think that's going to be the case somewhat indefinitely.
[00:10:56] Finally, although rust is definitely safer and more reliable than something like C++. Which is what it's competing with, if you don't need that level of performance that Rust offers. Like that C and C++ level where you might want to build a game engine out of it or something like really high powered It's not as safe as something like a pure functional programming language.
[00:11:14] Again like elm or there's a number of other languages out there that basically have even more safety guarantees than Rust, at the expense of not being as fast to run as rust. So that's why I said at the beginning like really the reason to use rust is if you really want to, you really want to go fast.
[00:11:29] You want to have that like top tier of performance. And there are some downsides that come with that. But as far as I'm concerned if you want to be in that tier, I can't think of anything I would rather use then rust. For most things, shout out to Zig Zig programming language is another thing that you might wanna consider.
[00:11:43] But it's a lot newer than rust. And not nearly as many people are using it, at least at this point.
>> What makes us compiler slow specifically?
>> Great question so what makes the Russ compiler slow honestly, there's not one particular thing. There's a number of design decisions, some of which are in the name of performance.
[00:12:02] Others of which are just that happened to be the way that they designed it. I wish I had a link handy for the blog post, but I guess there's not one short succinct answer to it. There's a guy who used to work on the Rust compiler that did sort of a deep dive into like why is Rust slow or like why isn't Rust fast to compile.
[00:12:20] Because of course the programs generates very fast or can be very fast. But I don't have that link handy offhand so I wish I had a quick answer. But it's a bunch of things, it's not just one thing. So putting these things together, we have some pros. We have some cons, like how does rust sort of stack up against other languages that people use?
[00:12:39] Well, one way you might evaluate this is stack overflow every year runs this survey. And they ask people what they think about the language they're using. Like what do they like about him when they dislike how much do they like them overall. And one of the categories they have at the end of the survey is what is the most loved programming language.
[00:12:56] And in 2016 that was Rust was considered the most loved programming language on Stack Overflow. Out of all the languages that people were using on Stack Overflow. In 2017, it was also Rust, also in 2018, also in 2019, and also in 2020. So basically the the past five years that they've been running the survey.
[00:13:14] Rust has been considered the most loved programming language by whatever survey methodology they're using. Now, does that mean that rust is the most loved programming language in the world? No, but it does mean there at least are a lot of people on stack overflow who are answering that survey.
[00:13:27] Who seem to think that It's a really nice programming language and is meeting your needs really well. So hopefully by the end of this course, you'll be able to evaluate for yourself whether it's a language that is going to meet your needs. Okay, so to recap, we started off talking about what is Rust who uses Rust, why and why not.
[00:13:42] You might use rust and the overall workshop structure