RustConf 2016 - Illustrated Adventure Guide

download RustConf 2016 - Illustrated Adventure Guide

If you can't read please download the document

Transcript of RustConf 2016 - Illustrated Adventure Guide

Welcome! Thank you all for coming today.

I really have to commend you, it took a lot of courage to sign up for this mission. This adventure were about to embark on

is not for the faint of heart.

So if any of you are pregnant

or nursing,

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 dont look quite like a seasoned ships captain, and youre right!

Im not. Im 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 before we get on the ship, Ill show you a map of where well be going. Right now, our ship is docked at the Port of Javascript, just off the coast of Rubyville. Well be sailing the seas of chunky bacon and should be landing at the Cargo Bay of Rustlandia in no time!

So lets get on board, take your seats,

no standing,

no eating or drinking, and most importantly

no staring at the captains eye hes really sensitive about it

Say goodbye to your loved ones, off we go!

If you all look out your windows to the west youll notice a beautiful sight, some foliage thats native to Rubyville - an abstract syntax tree! Its nodes are particularly lovely this time of year. Youre 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 youre used to if youre a Rubyist

but in Rustlandia well have to remember to compile our code before we can run it - otherwise it wont work! So when we get there, just remember two key phrases

cargo build

and cargo run. Theyll 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 wont know what the heck youre talking about.

Oh, I almost forgot to mention, on the way out youll notice a big pile of mains - dont forget to take one! Youll 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! Lets check out the town! Remember, things move a lot faster here, so be careful!

Check it out, its the Stack and the Heap! lets go inside. Youre 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 its 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 its 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. Lets check out one of the local eateries. Ive heard good things about this one.

Uh sir I cant quite read this menu im used to seeing things like

this

Instead I see all these like, Structs and Impls and whats this all about, huh? WAITER!!!

So sorry madam, I know this isnt something youre used to back in Rubyville but we here in Rustlandia, we um have no class

I cant believe it! This is heresy! How are we to get along without class? Its the one thing separating us from the animals!

Dont worry, dont worry at all - you can still get what you need!

here, here is the ChunkyBacon struct. Thats 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 ill be all set?

no no my good lady, for that youll need to write an Impl

thats an Implementation of ChunkyBacon. So if you want a new instance of ChunkyBacon

youll have to write a new() method yourself - it doesnt just happen automatically.

Customer - well then! Very good. Ill have a.

ill have a salad

and now, a word from our sponsors

HEY MACK!

Me?

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 dont know

HOW ABOUT

concurrency without data races?

I think???

WHAT ABOUT

memory safety without garbage collection?

That *sounds* good? I really dont

YOULL LOVE RUST

Welcome to Mutability Lake! Its a lovely day, so many of our distinguished townspeople are out sailing their toy ships. Some are fancier than others, some are mutable, some arent.

This one here is a nice one - this ones not mutable, so we cant 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 CANT DO THAT! Ah the compiler is yelling at us so we know we cant change this one. Its immutable. Lets try another one.

COOL! This ones definitely mutable. Lets just make a few changes before we return it.

PERFECT. Compiler what do you think?

Whats the matter, compiler?

YOU PUT WHEELS ON IT ITS NOT A BOAT ANYMORE ITS A CAR

YOU SAID YOU WOULD RETURN A BOAT YOU MUST RETURN A BOAT

ok ok

Ill take the wheels off. I thought it looked pretty cool though!

Hey THAT boat looks pretty cool

yoinch!

HEY! THAT BOAT DOESNT BELONG TO YOU!

Oh, sorry compiler! Who owns this boat?

i do

Oh, so sorry. Can I borrow it? I was hoping to play with it in my bathtub.

Well will you bring it back?

Of course!

go right ahead!

dont forget this! so everyone knows youre just borrowing it.

play music

hey thanks for returning my boat. compiler, does everything look good to you?

NO YOU ADDED A TUGBOAT THATS 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 isnt 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?

sure

well now you have one!

and now, a word from our sponsors.

hey kid

what?

DID YOU GET ALL THAT ABOUT OWNERSHIP AND BORROWING

huh?

THATS 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 arent really problems Ive had to deal with

WELL LET ME TELL YA IF YOU WERE A C++ PROGRAMMER YOUD BE REALLY EXCITED

ok

YOU BET ITS OK

can you just teach me something about Rust so the kids at school will finally think Im a cool guy?

SURE THING KID WELL TRY A LESS CONVOLUTED METAPHOR, WATCH THIS!

Hey again, travelers! Have you seen our esteemed library? Its 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 dont belong to anyone, you can just take them if you want and then theyre yours! Every so often people will come by with donations of books they dont need anymore, its great!

If you want to borrow a book you just use your ampersand

If you try to change a book while youre borrowing it

the compiler will yell at you and your code wont compile.

However, if you see something you like in the free pile, you can just take it, and do whatever you want with it - its 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 thats 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 hes done before you can borrow it

aw man

No complaining! Do you want a data race? DO YOU?

no

okay then

You might have noticed how clean and beautiful Rustlandia is, and yet you might have also noticed there are no garbage cans anywhere! Its actually because of that system of borrowing and ownership that Rustlandia is able to do without garbage collection, its a big part of what makes everything so fast and safe here!

But isnt it annoying having the compiler yelling at you all the time?

Hey now, dont judge the compiler so harshly look you hurt his feelings!! Hes not such a bad guy, hes 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, hes really a good guy. I promise!

and now, a word from our sponsors

TONIGHT ON WRST ITS 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 Ill 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 DONT YOU COMPILER

just because the code compiles doesnt mean its something you should do.

Results - for when something could go terribly wrong and you need to throw an error. Option - for when its 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 youre 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 youd 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, Ive 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 havent plugged in the story from the talk yet, right now its a little random. Ill 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. Im on Twitter at @_lizbaillie and thats it, the end