 Pay attention to this little animation there. That's the same animation that we had on rust fest if you were there Like very important very important detail took me ages to do that Was worth it totally worth it so Rust Question is What is rust and to be more specific? What is rust doing behind the curtains? So you might think you know what what rust looks like, but maybe you haven't looked below Behind the curtains you haven't really seen what rust will do with your code how will execute your code What are the limitations and so on the constraints and sometimes it's helpful to know your tools a little better So that's why I wanted to give this talk first of who am I And Who has seen a YouTube channel called hello rust like see a few people right? Wait, let me do one thing one. It was of course staged That's like unique because there's only one of these shirts because I never got around to start the campaign, but Yeah, one of you lucky people can win it and not not this specific one Or you might have heard from my blog who has seen this logo before More or less no one it's embarrassing So most of you Have no clue who I am and that's cool There's a funny story yesterday I was sitting here like in the middle of somewhere and I was sitting next to a guy and I asked him What he's gonna watch tomorrow and he was like, yeah, I will be at the rust talk for the first two There you are He was like don't point me out man And I said, well cool cool show me the thing about rust behind the curtains That's really interesting and he had the picture of me open and he didn't recognize that I was me So so I think that's a good thing because in reality, it's not about me. It's about rust. So I think that's cool. I Worked for a company called Trivago. It's a whole to search platform. We have a few million accommodations we have IT departments in this law of life take Palmer in Amsterdam and We do a lot of languages. So mostly Java Kotlin Go Python Maybe some rust. I haven't said that we do rust, but I also haven't declined that. So We have a tech blog if you want to check that one out, that's our campus and now you might be wondering why does Matthias have this pile of Things there on the table and if you can see from behind like I had the most creative idea to To push that one and so you can guess what it is as it's forming socks Pay attention to the manly leg on the top right side. That's my favorite part of it Yeah, they gave me some socks Wow And I thought like what am I gonna do with those? Well, I'm gonna throw a peep throw them at people Yeah, you can see me now better and I will do random questions throughout the talk no pop quiz, but then you can win socks and Finally before we start There's one thing that I forgot last time In fact, who was there last time last year you deserve a pair of socks right there This is how you sell those things otherwise No, that was a horrible mistake. I Already regret that idea wait So I forgot to mention my girlfriend Anna. So I hope she's watching now because otherwise it's her fault and Yeah, there we go. So I covered that part now. Let's go back on topic Why should you care about rust internals? We all have read the documentation maybe Some more pitches about rust here's one that was like one of the first ones we had So Ross is a system program that runs blazing fast prevents sack faults yada yada yada most people Don't care Because let me point out a few things here These are all terms That you have to know to understand what this definition means oops. Oh, I was off from the mic and This is not really approachable. This will put people off. They will say wow Why should I even care ice? I stopped reading after threat safety But there's also advanced type system garbage collectors static analysis Deterministic drops. What is a deterministic drop? Why is it important in a definition of a programming language? Well, it is important but Not to foster curiosity It's important to know what you can do with this language. That's correct, but You want something easier something more attainable something where people can relate to To to to foster their curiosity. So the new definition that we have I think it's much better It's empowering everyone to build reliable and efficient software No Deterministic drops in there No sack faults in there. It's just what rust can do. It's not what it is It is what it can do for you and that's a whole different ballgame Everyone is empowered to build reliable and efficient software and That's a really bold statement and that's a really cool goal. And this is why I like rust if you want to translate that into Matthias English, then I would say be curious try crazy crazy things. Don't be afraid You cannot possibly fail failure just means that you learn something You if you fall you always fall forward. So that's also a movement and he said that yesterday So I like this image from also again rustconf I guess this is Lucy and she built this steampunk rocket engine and she's flying around with it and that's really cool because She built it herself and it gets her it brings her to the next level right? She can she can go higher now That's that's what it's about So so we should we should aim for that We as a community we should aim to introduce that to the people so that they understand what rust is about Don't start with sack faults start with empowering start with curiosity Another one This is also from Roscoff 2018 Lucy is climbing high up there and This is kind of dangerous usually, but then again. She's safe because she's She's on this leash and if she falls she doesn't fall hard because this tiny little robot there that definitely can hold her weight He's gonna keep her safe right But still I like this metaphor because the human is in control the human decides where to go but the robots helps the human get there and It's such a nice. It's such a powerful metaphor Or as Pascal say it and then I will stop with those things He said being curious is an amazing trait. He said trait there. I don't know if that was a pun, but We should embrace it and help people be curious and This is exactly What I think it should should be about And how can you do that? How can you foster curiosity? There are people in this community and in other communities that do exactly this for example Who of you has heard of Julia evens? Sox, oh my god, you don't get socks. Sorry like the other kind of first if you lift your hand more than once Then you won't get so yeah. Well now you can now okay. You you also get socks, but then again That's it Those socks usually people don't care about the socks, but yeah, if you if you give them as a price So Julia evens is doing those comics about various systems things and she's making them approachable She's showing people what you can do with the language without being patronizing without being Very very low level she can do all of this with simple images descriptions and really I Like it a lot and I see a few nodding people. I think she's she's onto something there another example Who has ever heard of this chunky bacon? socks time One final socks time and then So who was it? Okay, just pick up your socks later Why The lucky stiff this is like the full name of this community member from the ruby community and he had wrote this book Why is porn and guide to ruby and there he describes? Ruby in a very different way in an artistic way with comics a lot of it doesn't make sense But in the end it all fits together. So I love this book as a child I read it a lot and I was like wow. This is programming. This is curiosity. This is like wow This is crazy. This is so out there And I wanted to try the same on rust and when I started with rust it felt like this little dog and I wanted to learn more about this Rust thing I was curious really I wanted to figure out what this was about but whenever I tried to kind of touch it or Dabble with it it would it would move or it would be different or it would not be Approachable and I would have a hard time to figure out how to get started What would be the first step to learn more about rust internals? And so the disappointing end was always that I lost track. I lost motivation I lost my goal out of sight and yeah That was that so another lost opportunity and I think a lot of people feel this way Maybe you also try to learn more about rust internals in like yeah when I get around to it or well I don't really I don't really have what it takes or yeah I have other things to do or it's too hard and For this talk then is for you. What's the rust compiler? Rust is written in rust It's a bootstrap compiler There are a few steps Throughout every rust compilation process first you take the rust source Then you desugar that into here the high-level intermediate representation and Then you take this representation and you more or less do you desugar it again? You do borrow checking there. You do optimizations there And then you have mirror, which is the medium intermediate representation Then you do more optimizations and you end up with LLVM IR Which is the LLVM back-end intermediate representation, which is more or less just bite codes So to say some operations similar to the Python interpret the bytecode you might think of it like this And then LLVM compiles it to machine code In fact rust is just a front-end for the LLVM compiler But let me focus your attention to the top of that part There's one more step which was recently introduced which is the hair Which is more or less similar to here But with some more optimizations being done and the reason why this and Also why mirror was introduced was that the compiler was very very hard to maintain and a lot of features We're very hard to implement without those abstractions For example, if you have ever heard of non lexical lifetimes This would have been almost impossible without mirror and It's good that we have those abstractions and it's also good that you don't have to see them on a regular basis But still I think it's good to know what those abstractions are so that you know what the compiler is doing so that it can You know interact with it and you have more control. You have more guidance with what you do D sugaring is the part of the compiler which takes your input and Produces output that the rust compiler itself can understand It's more or less simple rust. You can also write simple rust You can do it yourself But it would be very tedious and very hard to maintain and you would lose a lot of Functionality and convenience and and you know syntactic sugar So this is why the compiler does it for you it takes your code and it transforms it into something healthy for the compiler We all love vegetables That's why I have those vegetable images not because I did not have any other idea for this slide and The compiler does everything for you. You might say Matthias, okay We're in this talk for a while Can you show me an example? Yeah, I'm glad you asked Let's let's look at it an example So the first example imagine a perfect world The only bug-free program an empty main. This is what you write This is not what the compiler sees though Maybe some voices anyone knows what what is missing there in the slide when the compiler runs there Yes Yes, yes an arrow and open close parentheses. Yes Some more maybe but the most important part is When you use rust and you use a string Well, you have never imported the string Did you ever did that ever cross your mind? You use a vector, but the vector you never said use standard collections vector, right? So there's something going on there and That is What they call the prelude? one thing is the standard library Those are all the things that You'd need on a daily basis and the prelude includes stuff from the standard library So that's why you need both parts The standard library is just another crate It's just something that you can compile into your binary But it can also run a no standard build and that's also completely fine Then you don't have allocators and all that functionality from standard library and This prelude includes all of this you see those the array you see those Tags above the imports the prelude import That's just for the compiler to know that this is some kind of a special import. You cannot really do that yourself This is like an unstable thing That's why we also have a version one because there might be a version two of this prelude and so it's a smart idea and What will you get when you just include this prelude? You will get all of this a Few types a few trades and one function The types are let me clean it up a little bit for you the types are Things that you need in almost every Rust program Option and result string and vector and some of you might have heard of a box Which is just something like a smart pointer you have something on a stack that points to something on the heap and More or less everything that is dynamic like a string or vector is also a smart pointer and That's quite a helpful abstraction You have all those partial equal and partial ordering trades and so on and that can group that up even more for you The first part of set of trades is for ordering things the second part is for converting things From making it a reference making it mutable converting it from or to another type into a string cloning it and so on Third set is for just a default value of your thing Sometimes very helpful for example the default of String is an empty string or something Then something like an iteration Then you have those market trades which get optimized the way at compile time Those are just hints for the compiler to tell you what to do with your code So you declare this as being able to be copied or this being able to be sent over to a different thread Then you have a few trades for as I call it calling or dropping objects Yeah, a lot of things are callable and Even things that you might say like new is something that you call an fn on so you need those trades in the scope and the last thing is kind of the weirdest thing but It's for concatenating objects more or less like strings and vectors when you want to add things to those vectors You need that all the time Okay That's that's nice, but not really code that you would write yourself Let's look at a concrete example for code that you would write that then we can de-sugar and see what's going on So just keep in mind that there will always be this part of the prelude in every Rust program We don't care about this anymore now. We just care about concrete code and Yeah Who knows what this will print Nothing yes Yeah, I'm stupid, but So the iteration goes from Zero to two. Yes, exactly. It's an exclusive range What you could do is you take this part and you make that a little bit more explicit So maybe you move that into a separate Variable and then you take that and you make it even more explicit and then you call a struct Creation on it so range will get a start in an endpoint and this is exactly doing the same thing Well Let me move that over a little bit because we need more space first thing we need is import our Operations range because that's not in the scope remember it's not in the prelude So we have to import that ourselves and then when we look at this loop. We are not really done yet We can optimize that loop and if you were right writing a compiler You were optimized it a little more by saying okay. I don't really need these for I in range. I can also say While let some equals it or next so that's a lot to take in but You can see that we create an iterator from our range at Line above and then we call while let some equals it or next which means as long as there's a next element Do something with it in the loop Just one way for the compiler to not handle the four stuff it can map that to a while loop, but even that is very very Sugary for the compiler so it will do more and every while loop can also be translated into Exactly a normal loop so a loop over an infinite range more or less or in an infinite loop and then in every iteration we check the iterator and If there's an element we do something with it and if there is no element. Yeah, we break Well, that's awesome. How did I figure that out? turns out that the compiler allows for inspecting this and It's no it's no no secret, but it's also kind of hard to To start with that so that's why I wrote a little tool called cargo inspect Which will do exactly this for you you give it some code and it will print what the compiler will see and If you want to follow along You can just cargo install cargo inspect and then call cargo inspect on any rust file or any crate and Now here's one more sock time because I need someone to hold the microphone while I type Look at it. That's so nice of you. Well, like, yeah This is not gonna work. I appreciate that you try to have very long arms, but Okay, that's awkward because My screen is not really mirrored That's horrible. Ah Yeah, so Let's say I have a crate here And I have I'm typing this blindly And and I have something like very very simple here Like a normal opening of a file. Ah, that's better. I Can now say cargo inspect and It would just It would just unroll whatever The rust code is doing behind the curtains So you can see colors and syntax highlighting and pagination everything works out of the box Yep, thank you. I will need you more often Inclusive ranges Well, if you look at this example, it's similar to the other one with one minor difference Who can spot the difference here? Yeah, it's easy. Okay. It's an equal sign and What this will compile to is that and Where's the difference here? Yes, it's the range inclusive It's the more or less the same thing But yeah, it's an inclusive range So you might say why if I if I if I iterate my own over my code That's kind of very hard to spot, right because I have this one example I want to compare the outcome with another example. So how can I compare those outcomes? How can I compare what the compiler is generating? And so that's why I added a feature called cargo inspect minus minus div and you can give it two Input files and it will generate the diff for you. So this is very experimental Yeah, you need to hold the thing again so professional I Would love to mirror that screen right now Wait, wait, I have an idea so you can see you have two examples one is range and the other one is range inclusive and Now if you run the diff command on it You will see the outcome and there's some problem with the syntax highlighting right now So that's why I need an additional flag But I'll fix the syntax highlighting soon ish. You can see that It's more or less Yeah, explaining to you what changed when you change this one Letter, so it's kind of easy to spot then what's going on in the compiler as you iterate you can even set a watcher for it and then You know interactively do that output, but let's Yeah, not focus on that right now. I think it's it's really helpful though. Yeah And You have already seen the compiled output of this example But I want to go through this step-by-step and tell you what's going on. So this is a very simple example for opening a file Okay We say let f equals file open file dot txt question mark and we have an ok value First of why do we need an ok return value because this function will return a result you can return results in main now And there's two cases either this operation will succeed in which case we return this unit type Or it will fail and we return an IO error So far so good But could you explain what this question mark operator does internally? That's the carrier operator Fun story. I talked to Niko Matziakis who implemented that I said Like how came you up? Did you came up come up with the carrier operator? He said what is the carrier operator? So apparently it's just me you using that term, but okay fine That carrier operator this question mark here is really peculiar Because if you were at Involved in rust for a longer time you would know that this what was not really possible before this question mark operator There was a try macro and before this try macro even there was syntax to make this error handling very explicit and So what what do you have to do in this case? You know that the file operation can fail And you have to check for this condition file open will return a result and The result can have two options. Okay, or error So what you could do is you could match on that result and say If it's an okay file, then just assign file to F if it's an error Assign Yeah, actually return an error. So don't even assign anything So we're done right Well, what happens if Our error type is not really the error type that we return from these functions in this case It should be an IO error But as you know, you can use the carrier operator for many many other errors Including your own errors and how does the compiler know what to do in this case? And there's a very very smart thing that is in the compiler Which is the from conversion? As long as there's an implementation of the from trade Or actually into of the from trade because into is implicit As long as there's this conversion going on you can call it and the conversion will happen at compile time with all the safety features So that's what the compiler is really doing. It's converting this error Into a from from error. It's calling from on your error And if there's no from then sometimes you will get this weird error message saying Into something is not defined blah blah blah for this type and this is what's going on It's trying to call from but your trade Implementation just doesn't exist and that's super helpful in in many many different cases and Very powerful metaphor Okay, let me show you Again this this code that we have before and I don't need look for that. I do need look for that again, I guess Yep, okay, so you can see it's running on a cargo crate it was running on a cargo crate because that's an entire crate just to show off that it can also use crates and This is more or less the Fully explicit unrolled de-sugarred File opening code that you saw before The only thing I did was removing some of the includes Yes, one second you will get the microphone back in a second Yeah, now Most of the time you don't work on the IDE you use your actually you use your IDE You don't work on the terminal for for interacting with your code So why not use this functionality from your IDE hold it I got you covered So let's create a file let's call it test RS and Let's let's just write some rust code, okay Now let's see what the compiler is doing. I Just opened my IDE and I say cargo inspect run It's gonna produce this output for me In a separate tab you can see how it print is implemented. How cool is that? How can I move tabs from left to right? Why have I not tried that before? Close tabs to the no that doesn't work close others close others No No, no, no, that would be too easy guys. Okay so this is print line a lot of boilerplate, but You can understand it if you really Squinch you can see that we call IO print on some operations We match on X we we ask if X has a display implementation or actually no we ask if if there is an X and if so we formatted with display and then all of this boilerplate is just the print line macro, but The main part is you can do all of this from your IDE You don't even you don't even have to use the console if you don't want to Where was I yeah Another example you can't definitely not read that but it's a vector and Sorting on a vector on the right side. You can say you can see what's going on There's it's it's running into back on it though. It converts your macro into an FN Yeah, I think that's cool Turns out there is no support for Italy J right now Yes, yes, I feel with you Now I'm a genius right I invented rust tooling so No, of course not there are many other people that did a much better job than me and just look for a few other crates and try them First of there's cargo expand which is Really good It's more focused on macro expansion though But more or less we use the same built-in functionality now We also use the print pretty print create for printing this stuff and maybe those projects will emerge in the end Then there's cargo asm which shows you the assembly code and I have I have an example open here I don't need a looker Why is it that I always move to the different other yeah, yeah So that that's the file opening thing again or running on cargo asm and This is the assembly code that the compiler will Generate for you not going to go into that but that will be a whole different talk I'm just saying that you can do all of this with with very very simple tooling that is readily available for you and Last but not least there's also cargo bloat which will show you What functions take up the most space in your binary and how much? Storage you could save by removing those functions all of those including cargo inspect Are very nice to get to know the compiler I also advise you to just compile the compiler once there are instructions in the Rust c-book and Debug it just give it a shot and if you say yeah IDE tools and terminal tools are not really mine Just try the same thing on the playground You can print the Here you can print the mirror you can print the asm all from your browser and Finally, I want to show a give a shout out to this guy Got bold which is more or less by now a verb for inspecting compiler internals On this website you can not only compile rust code, but also c++ and many many other languages and It's just it's just crazy So to wrap it up. What have we learned? Rust allows for a lot of syntactic sugar but it's sometimes good to be reminded about that and to know what's going on behind the curtains and Tools can help you understand what's going on and you should write more such tools and use such tools and That's why I say go and build awesome and cool things and If you are really in the mood to to get your feet wet Then there are a few issues That you can take a look at I don't think many of them will be really hard And so those are good for it for starters. The code is super simple. It's more or less a few hundred lines so really no big deal and There are a few things that people are missing and so on and so maybe if you want to contribute Please feel free and talk to me after the talk that will be great Finally some credits and also look at the rust compiler guide. I will upload the slides somewhere In case you you're one of the people that keep asking me for another episode of hello rust. Yes, it's in the pipeline Yes, I just have to get around to to do it and yeah With that, thank you for your attention. I guess we have time for two questions or so question is The question was how can I get thrown out of this room? Yeah There will be socks. Yeah, pick up pick up your pair even if you haven't answered any questions I want you to to use that as a reminder that you can do all of this and always wear those socks when you're Programming rust That there are even there are even gears on it. So Wow Yes Yeah The question was have I tried expanding know that the no standard create? No It turns out that if you have very very big code The compilation the output it will be quite substantial. I haven't even so that's why I used small small Snippets, but you can definitely do that I mean I tried compiling very very big crates and sometimes by accident and then I was like wow What is all this code? But yeah, you can look at the STD. It would be a nice thing to do really Maybe you can even yeah Maybe you can do it Okay, so the question actually wasn't really a question was a statement The it was a feature request To implement source code maps that are also common in Java and make it available from VS code so that people can inspect What's going on where especially when you cross compile more or less cross compiling? Yeah No, not cross compiling not really of course not, but yeah, maybe create an issue and then we can figure it out maybe sure and Also someone has to make an IntelliJ plugin for it. Otherwise, I will be very sad Okay, I guess that sums it up. Thank you very much