I really have to commend you, it took a lot of courage to sign up for this mission. This adventure we’re about to embark on
is not for the faint of heart.
So if any of you are pregnant
or suffer from any kind of heart condition,
or if your doctor has advised against the eating of spicy foods,
I ask that you consider consulting your physician,
spiritual advisor, psychic medium,
or nosy next-door neighbor before accompanying me on this journey.
While we wait to board the ship, some of you might want to know a little bit about me, your guide, before moving forward with this seafaring journey, and I get it. I understand.
I don’t look quite like a seasoned ship’s captain, and you’re right!
I’m not. I’m Liz.
I used to be a cartoonist,
I went to art school,
drew a few graphic novels.
A few years ago I learned to code,
went to the Flatiron School in New York and started working in web development.
Nowadays I work at Tilde right here in Portland, OR.
Mainly I work on our product, Skylight, which is a Rails profiler that helps developers optimize their Rails apps.
Fun fact: we also use Rust, which is a big part of why I started learning Rust in the first place.
So let’s get on board, take your seats,
no eating or drinking, and most importantly
no staring at the captain’s eye he’s really sensitive about it
Say goodbye to your loved ones, off we go!
If you all look out your windows to the west you’ll notice a beautiful sight, some foliage that’s native to Rubyville - an abstract syntax tree! It’s nodes are particularly lovely this time of year. You’re probably used to seeing these in Rubyville,
they tend to sprout anytime some code gets thrown into the interpreter, just before it gets turned into byte code so the Ruby Virtual Machine can run it. As you might already know, Ruby is an interpreted language, so this is more or less what you’re used to if you’re a Rubyist
but in Rustlandia we’ll have to remember to compile our code before we can run it - otherwise it won’t work! So when we get there, just remember two key phrases
and cargo run. They’ll come in handy when we reach the shore and start trying to chat up the locals.
If you try to just run your code directly like you did in Rubyville,
they won’t know what the heck you’re talking about.
Oh, I almost forgot to mention, on the way out you’ll notice a big pile of mains - don’t forget to take one! You’ll need to put all the code that gets run inside of main. Very important.
Alright everybody off the ship, here we are! Welcome… to Rustlandia! Let’s check out the town! Remember, things move a lot faster here, so be careful!
Check it out, it’s the Stack and the Heap! let’s go inside. You’re probably used to not giving much thought to memory back in Rubyville.
Before I came to Rustlandia I had heard of the Stack and the Heap and I knew it had something to do with memory but never really understood it.
Watch how things work at the bar. People come in and give their programs to the bartender, and she compiles and runs them.
Someone wants a fancy whisky and a cheap whiskey. The good stuff is fancy so we put that in a Box and store it on the heap, because we want it to be able to stick around for a while, even though it’s high up and a bit slower to get to. Most things in Rust are stored on the stack unless you specify otherwise, which is why we had to put the fancy whisky in a Box in order to store it on the heap.
So when we run this program, the good_whisky gets a spot on the heap, at the far far end of our available memory,
and we put a pointer on the stack that points to that spot on the heap. Next on the stack we put the cheap_stuff. The cheap_stuff gets served up first because it’s on top, and then we have the pointer to the fancy stuff, which the bartender has to go way way up to the top of the heap to get, which she does.
All that sea travel made me hungry I think. Let’s check out one of the local eateries. I’ve heard good things about this one.
“Uh sir I can’t quite read this menu… i’m used to seeing things like
Instead I see all these… like, Structs and Impls and… what’s this all about, huh? WAITER!!!
So sorry madam, I know this isn’t something you’re used to back in Rubyville but … we here in Rustlandia, we um… have no class
I can’t believe it! This is heresy! How are we to get along without class? It’s the one thing separating us from the animals!
Don’t worry, don’t worry at all - you can still get what you need!
here, here is the ChunkyBacon struct. That’s where we define all the attributes we expect out of it - flavor, chunkiness, price, etc
so then I can just do something like
this and i’ll be all set?
no no my good lady, for that you’ll need to write an Impl
that’s an Implementation of ChunkyBacon. So if you want a new instance of ChunkyBacon
you’ll have to write a new() method yourself - it doesn’t just happen automatically.
Customer - well then! Very good. I’ll have a….
i’ll have a salad
and now, a word from our sponsors
YEAH YOU THERE. YOU WANNA TRY SOME RUST?
You mean the iron oxide produced as a result of a redox reaction of iron and oxygen in the presence of water or air moisture?
WHADDAYA BEEN READING WIKIPEDIA? No NOT THAT RUST FOOL!! RUST LANG MY FRIEND!
Oh yeah that cool new systems programming language all the kids at school are talking about?
THE VERY SAME.
well, what about it?
DO YA LIKE
abstraction without overhead?
uh I don’t know
concurrency without data races?
memory safety without garbage collection?
That… *sounds* good? I really don’t…
YOU’LL LOVE RUST
Welcome to Mutability Lake! It’s a lovely day, so many of our distinguished townspeople are out sailing their toy ships. Some are fancier than others, some are mutable, some aren’t.
This one here is a nice one - this one’s not mutable, so we can’t change anything about it, but I can show it to you! Look how nice! But if we try to change anything about it
HEY YOU CAN’T DO THAT! Ah the compiler is yelling at us… so we know we can’t change this one. It’s immutable. Let’s try another one.
COOL! This one’s definitely mutable. Let’s just make a few changes before we return it.
PERFECT. Compiler what do you think?
What’s the matter, compiler?
YOU PUT WHEELS ON IT IT’S NOT A BOAT ANYMORE IT’S A CAR
YOU SAID YOU WOULD RETURN A BOAT YOU MUST RETURN A BOAT
I’ll take the wheels off. I thought it looked pretty cool though!
Hey THAT boat looks pretty cool
HEY! THAT BOAT DOESN’T BELONG TO YOU!
Oh, sorry compiler! Who owns this boat?
Oh, so sorry. Can I borrow it? I was hoping to play with it in my bathtub.
Well… will you bring it back?
go right ahead!
don’t forget this! so everyone knows you’re just borrowing it.
hey thanks for returning my boat. compiler, does everything look good to you?
NO YOU ADDED A TUGBOAT THAT’S NOT OK
but compiler, these boats are all Vectors of Strings, all I did was push a tugboat onto it. A tugboat is a string!
the tugboat isn’t yours and neither is the boat!
Yeah that tugboat is mine
well what if I just….
BOOM punch the tugboat guy in the face and hit him with a remove!
looks like no one owns this tugboat now
you want a tugboat on your boat sir?
well now you have one!
and now, a word from our sponsors.
DID YOU GET ALL THAT ABOUT OWNERSHIP AND BORROWING
THAT’S HOW WE GET A LOT OF THE COOL STUFF I WAS TELLING YOU ABOUT BEFORE
you mean like when you were yelling ‘memory safety without garbage collection ‘ at me?
YES YOU GOT IT
I only know Ruby though so those aren’t really problems I’ve had to deal with
WELL LET ME TELL YA IF YOU WERE A C++ PROGRAMMER YOU’D BE REALLY EXCITED
YOU BET IT”S OK
can you just teach me something about Rust so the kids at school will finally think I’m a cool guy?
SURE THING KID WE’LL TRY A LESS CONVOLUTED METAPHOR, WATCH THIS!
Hey again, travelers! Have you seen our esteemed library? It’s pretty great. You can borrow just about anything, as long as you return it,
they even have this great big pile of books over here that don’t belong to anyone, you can just take them if you want and then they’re yours! Every so often people will come by with donations of books they don’t need anymore, it’s great!
If you want to borrow a book you just use your ampersand
If you try to change a book while you’re borrowing it
the compiler will yell at you and your code won’t compile.
However, if you see something you like in the “free” pile, you can just take it, and do whatever you want with it - it’s yours! You there, in the back - you had a question?
yes what about if the book is a mutable reference?
Great question, friend! Well, you might very well be borrowing something that’s mutable (&mut) like our collection of coloring books here - you can continue coloring in them while you have them and you can return them altered. Only one person can have one out at a time though.
We also have some Exquisite Corpse books that are pretty cool - every time someone borrows one of those, they add a little bit to it themselves before bringing it back.
I want to borrow the exquisite corpse book!
me too! I want to borrow it too!
No more than one mutable reference at a time. You can have it now, but you have to wait until he’s done before you can borrow it
No complaining! Do you want a data race? DO YOU?
You might have noticed how clean and beautiful Rustlandia is, and yet you might have also noticed there are no garbage cans anywhere! It’s actually because of that system of borrowing and ownership that Rustlandia is able to do without garbage collection, it’s a big part of what makes everything so fast and safe here!
But isn’t it annoying having the compiler yelling at you all the time?
Hey now, don’t judge the compiler so harshly look you hurt his feelings!! He’s not such a bad guy, he’s just making sure everything we do is good before we can run it.
The compiler is our friend who just wants the best for us! Sure his advice might be a little hard to understand at times but once you get to know him, he’s really a good guy. I promise!
and now, a word from our sponsors
TONIGHT ON WRST IT’S THE TRUE STORY OF HOW ONE STATICALLY TYPED PROGRAMMING LANGUAGE RISKED IT ALL TO FIGURE OUT HOW TO HANDLE IT WHEN THINGS GO WRONG.
“If everything goes right, the reactor will cool down and the city will be saved. But if something goes wrong… if this whole thing goes south
well I guess I’ll just do nothing . I mean this seems like the time to return an Option right? I mean the compiler feels fine about it right?
meanwhile, across town
the grocery store does not have the mustard i like!
RED ALERT RED ALERT ALL HANDS ON DECK SHUT EVERYTHING DOWN I NEED MY DIJON!!! COMPILER AGREES DON’T YOU COMPILER
just because the code compiles doesn’t mean it’s something you should do.
Results - for when something could go terribly wrong and you need to throw an error. Option - for when it’s ok to just do nothing.
Hey welcome back!! Just in time for the last boat back to Rubyville. I sincerely hope you enjoyed your stay and that you visit again soon! If you want to stay a little longer there are some very nice Boxes at the Heap, otherwise the boat is ready to board!
On your way back we do have some very nice reading material for you if you’re interested in learning more about Rust: I strongly recommend starting with Rust by Example, this online book will lead you by the hand step-by-step through many examples, explaining everything along the way
After that I recommend looking at the official Rust reference documentation, very helpful
If you’d like to try your hand at some Rust of your very own from scratch there are many excellent exercises available at exercism
If you have questions, there is the Rust Reddit and the user forums, or you can chat on one of many channels on IRC
In preparation for this talk, with some help from Yehuda, I’ve been working on a playable text-based adventure game version of this talk in Ruby (which I am also porting over to Rust). This way you can check out Ruby and Rust code side-by-side, but you can also actually play the game. Check it out at https://github.com/tildeio/learning-rust. The Rust code is still in development and most of it is on a branch called just-rust-things, but the Ruby version is on master and is currently playable, though I haven’t plugged in the story from the talk yet, right now it’s a little random. I’ll be doing a lot of that in the coming weeks, so feel free to check it out.
THANKS - last but not least I want to thank Tilde for allowing me to work on the Ruby/Rust game on company time, and Yehuda for working with me on the Rust side of the game, patiently answering my questions and helping me when I got stuck.
Once again, my name is Liz. I’m on Twitter at @_lizbaillie and that’s it, the end