Rust for Web Developers | Mozilla ♥ Rust
Articles,  Blog

Rust for Web Developers | Mozilla ♥ Rust


Let me tell you a little bit about what Rust is about. Rust is a secret weapon Rust is a community project. Rust has learned from the mistakes of the past. Rust is a systems programming language that is safe, concurrent, and fast. I got one word for you: Rust. Rust make systems programming accessible web developers. When you need to optimize your web application, instead of diving to C, which is incredibly error-prone, you can instead use Rust, which is going to protect you from errors and it’s going to be as fast as the hardware allows. There’s a large group of people who traditionally would use higher-level languages like JavaScript or Python or Ruby, that run into performance walls in the code that they’re writing where they need to do something lower level… So something like node.js or Python or Ruby where you might not have all the low-level control to do things like talk to hardware if you’re doing some sort of embedded device or use different APIs or networking protocols that might not be exposed. And one of the promises of Rust is that you can plug it into existing systems, and for the part of your app where you really need to tune the performance up, you can rewrite just that part in Rust. So I think part of the initial promise for people who are just coming to Rust from other ecosystems is they don’t have to jump in with both feet. They can give Rust a try just for portions of their app, and still keep the rest of their app as it is. Rust safe systems programming is a big deal because it brings this kind of programming to the masses, which was never possible before. Rust is about letting people do the same kinds of programming in C and C++ except that when you break, the rules you get an error right away. An automated mentor sitting there beside you saying “Oh, that’s going to end poorly if you write your code that way,” and then not letting you go on until you’ve fixed it. No matter where you are along the learning process you never have to be afraid of shipping code. You don’t have to worry that you’re going to ship code in production and there’s going to be some security exploit, or some segmentation fault because there was some detail of the language that you hadn’t learned yet. When you talk about lower-to-higher level languages, one of the distinctions is that a lower-level language will typically need a whole lot more ‘typing’. ‘Types’ are a way of expressing your intent to the computer so that it can actually check the code you wrote against your stated intentions. JavaScript programmers and Python programmers are not used to having to do that at all. They write the code, and it runs. With Rust you actually have to – it requires you to put in the effort to write out your expectations for what these values are going to be upfront. And the payoff is that you get much better performance and you get much better consistency checking. Rust will catch all kinds of mistakes in your program that a Python system or a JavaScript system wouldn’t catch until you ran your your unit tests. Rust comes with a standard package manager called Cargo, and the entire ecosystem uses it. If you’re coming from JavaScript you’ve probably used a package manager called NPM, and Cargo is essentially the same thing, where it enables it enables you to pull in all this code that has been written and vetted by others, and it makes it incredibly easy to do so by just changing a few little configuration files. Rust users consistently point to Cargo as one of their favorite parts of the experience of Rust programming. We made the conscious decision that it wasn’t enough to just ship a programming language. We needed to also ship a tool that would allow people to share their libraries in a consistent, uniform way. If you want to write OpenGL code, you can just find a library that’s already been written so that you can do OpenGL code in Rust. Quick and easy, and in one place. One of the projects I’ve been working on is a system called Neon that allows you to plug Rust into a Node.js app. So let’s say you have a Node app and you’ve noticed that one part of the app really could stand to be optimized further, and you’d like to rewrite that part in Rust. You can use Neon to plug that in. Another exciting way that JavaScript programmers are hopefully going to be able to start using Rust pretty soon is by compiling Rust to the web using the emscripten compiler. With emscripten you can compile your Rust app to asm.js and you can also compile it to webassembly, which is an emerging standard for high-performance web applications. So, part of what’s exciting about this is it’s giving JavaScript programmers access to the highest performance part of the web platform, and it’s also giving them an opportunity to experiment with Rust, and get started learning how to do Rust programing. If you learn super well by reading a textbook, go read the Rust book from cover to cover. It’s free and online. If you learn better from trying stuff out, find a repo of simple exercises that come with their answers. If you learn best from working directly with other people and from pair programming, go find your local Rust meetup. They’re all over the world. A great place to look for your first Rust code to write is the Servo project, which is building a next-generation web engine, and is a community that’s very big on helping newcomers get started with Rust programming. The way that you will buy this improved performance and this improved safety is going to be with some frustration and some study, but someday you’ll find yourself teaching a new Rustacean about how the language works, how they can make their own trait, how they can debug that particular error and make it go away, and you’ll realize you’ve made it. This is easy now. Come to Rust and you feel like you suddenly have superpowers. I love Rust. Congratulations on starting Rust. Go try Rust.

19 Comments

  • slier81

    the problem with rust its adopting rate..and its development cycle…it takes like forever for rust to get out from beta to stable version 1..thats quite scary in programming world

  • Ian Macdonald

    I just think we should be moving away from C syntax entirely. It's already 40 years past its sell-by date. The only reason it was chosen for the puny first IBM PC was because of its extreme terseness, an advantage on a machine with a tiny amount of RAM.

    Even in the 1950's using vacuum tube computers, they had programming languages that were close to human language syntax. The choice of C was actually a backward step, even then.

    The key problem with C syntax is that with every delimiter being a brace, errors are easy to make and hard to spot. If meaningful words are used, the errors become easy to spot. That said, you make far fewer errors anyway so you spend less time debugging.

    It's extremely rare to have a C, PHP or Javascript function work first time. There are BOUND to be punctuation or case-sensitivity errors. Using a case insensitive and English-like language, it is not all that unusual to have a 100-line block of code work perfectly first time The result is quality code that gives a satisfying user experience.. instead of bugs, bluescreens and aggro.

    That's even before you get to writing a manual for your code, and run into all kinds of grammatical issues caused by case sensitivity. do you write lousy grammar, Or instructions that don't work? your Choice.

  • Detective Vermin

    Can we have one, ultimate, derivative, language that defeats them all! Is that what rust is? I want to learn how to create programs, but I am really sick of the bullshit! The web is where it is at, so javascript(interpreter based languages ) are where we are going to lean. Low level code? Why? What is the answer?

  • zyxwvutsrqponmlkh

    Yeah but the community is full of dogmatic ideological cancer. These are the types of people who will refuse your commit if they find out you support Trump.

  • Sayan Nandan

    I now find it pretty compelling to give rust a try…..I'm coming from a C# background and the syntax seems a bit weird, but I'll get onto this engine for my web app. BTW, is rust supported for deployment to the cloud? (Like EC2, I'm talking about available APIs)

  • Mr Anderson

    @ 1:07 they remove a fitting cog with the rust cog that wouldn't spin anything… OCD just saying. Not criticising Rust though, Rust seems like an amazing language.

Leave a Reply

Your email address will not be published. Required fields are marked *