 Testing testing testing testing All right, it's three o'clock so I'm gonna start it. This is rust system programming for web developers Before we start out, I do have a confession to make. I am a rustation So if you write Python, you're a Pythonista if you write Ruby you're a Rubyist if you write rust your rustation Not rustifarian Rustation and this this little guy on my shirt is the official rust mascot a little crab or rustation and That's what I do at night If you write PHP, does anyone know what what do you are if you write PHP that'd be misguided Actually, I kid I write PHP during the day. I've been doing it for 10 years, but I love rust I fell in love with this language and so I work at Norsham rack comm slash hope look during the day writing PHP and managing and I write rust sort of on the side So before we get into rust itself, let's actually talk about what system programming is There's no definition of everyone's in a grana about what system programming is and those of us in the rest community seem to have Coal us around this definition of system programming is when it matters So I'll give you an example of when it doesn't matter I'll pick on Ruby this time Ruby is slow, but it doesn't matter because the database is a bottleneck So when you're writing Ruby code, we're not calling that systems programming Right because it doesn't actually matter if Ruby's a little slower Than where you're adding it in C because you're bottlenecks the database, right? There are a couple characteristics that you're gonna sort of see if you're a system programming You're not gonna have a garbage collector You're gonna have to manage memory yourself. You're gonna find great control over your memory management So we see that a lot in C or C++ and you're gonna have zero cost Abstractions so that basically means is you're writing code that's sort of against the metal or against the hardware, right? You could be flipping registers on and off if you want to you can Represent a data structure in the exact size of memory that you specified You can make entire programs sort of allocate memory on boot and then allocate nothing afterwards running just on the stack And then free up at the end. So you have a lot of control So that is systems programming and let's talk about why I think rust is a great systems programming language And why I think web developers should take a look at it Start with some history. So rust is a language that's sort of been developed by Mozilla It was developed with the primary use case in mind see a Mozilla runs as you all know the Firefox web browser and Since the web is such a popular place nowadays There's lots of events like Pone to own or a bunch of security researchers or hackers will get together and they will Present their exploits and they do this with Firefox and Chrome and every single browser out there and Mozilla Sort of knows this pattern where all the exploits were basically coming Centered on this sort of one vulnerability, which is a data race And now it sort of happened when they would sort of have all these sort of threads running You know, they write C++ all this threaded programming going on to make Firefox fast even though Chrome faster, but they got all these threads going on and There'd be sort of some some mistake that someone would make with the way the threads are working and sort of sharing memory that would cause a data race and hackers would take advantage of that and make the web browser do things like a remote command exploit or They would allow it to make it do something like going to like a web page or fetching a resource that it shouldn't And this was driving Mozilla crazy. They wanted to fix this So Russ primary motivation is to sort of fix this problem that Firefox is having about sort of not having some sort of safety Checks around this because they're programming even for the most experienced developers super hard So what they did is they developed the working and developing now a web browser engine called servo Which is sort of a replacement to gecko So if you don't know Firefox uses the gecko rendering engine to actually render out the HTML and the CSS On your web page, so they're not really building a new browser But sort of building a new engine we actually already see elements of rust and Servo kind of being feathered into the existing Firefox browser So before we get too far to we're talking talking about your web developers, hopefully is anyone here actually written a rust program before So a fair amount of rush stations in the room. That's great Welcome, but if you're a developer, you might be asking yourself this question Is rust good for web development and the rest community sort of has this pension for making these websites called like are we blank yet? comm and Are we get comm you can actually go this next talk about? writing a web browser Or say web service in rust And you can do this, but I think it's too early to tell if web of rust is me good for web development Rust is primarily assistance programming language. We're talking about that and really the problem with rust in the web development Context is that the web's made up of strings HDP is a bunch of strings coming in your database connection as usually a Select with a bunch of strings coming back out To your Ruby or PHP or Python code and really converting all those strings to types. Normally isn't worth it, right? web request we hope are under like 200 milliseconds We're trying for expressiveness in our web code. We're using GC to kind of save ourselves our memory management a lot of times So it's just really not worth it to sort of do that Also, we don't really need fine-grained controller for memory because again We're only 200 milliseconds is our span. There's some memory leak or somebody overhead the request is gonna be finished up quickly Anyways, and we'll start over again Also, rust does is a is a compiled language. So there's a compilation step So we're not really gonna focus on rust for web development. I'm gonna sort of slant this over to Why we have developers to sort of look at actually getting involved in a systems programming language Yep, so rust possible developer. I contend is gonna be a can be a systems programmer, right? So let's talk about this. I think I did actually skip a slide So rust sort of provides these three constraints, right memory safety without garbage collection concurrency without data races and Abstraction the overhead which we talked about is sort of what makes up system programming and we're gonna sort of go through these And how they can be applied to web developers So I sort of have this this basic theory, right any time a new programming language comes out These bunch of people will get on the internet and they'll say something like oh this language is gonna replace all the use cases for this other language Right, we saw that with rust too rust was sort of coming out. It was eight years in the making. It's at 1.6 now People were sort of discussing a lot. Oh, do you think rust is gonna replace your C++? I don't think that's really actually the case if you're writing C++ right now You're probably pretty comfortable in a language that you've been writing for years and years and years and you might switch But a lot of people like they have no reason to switch they're working a day job using C++ and they sort of invested all this time energy and getting Familiar with that and they can sort of you know work through that. I actually think that Rust won't be a C++ killer and I think Instead web developers are sort of the largest group out there right now of people in like the software developing community Lots of new people coming in and I actually think rust is sort of well-positioned given some of its features to actually be attractive To developers and actually grow the overall pie So there's really not a focus on actually cannibalizing that existing blue graph blue pie chart area of C and C++ developers, but I'm actually arguing like I think rust actually can put a nice slice of Of new systems programmers into the community, which I think is sorely needed I think there's some historical context to this, right? I just didn't like make this up out of thin air. We've sort of seen this so Does anyone sort of was programming when Rails came out ever the same thing happening where there was a bunch of Debate going on in the web development community talking about like would rails would Ruby on rails sort of you know Unseat PHP is the dominant web language would it sort of take over and you see these You know blog posts flying back and forth rails is faster and PHP is faster and sort of it's never ending right and their benchmarks benchmark slide But what no one sort of understood at the time is like cloud computing is in its infancy, right? Like easy to starting to sort of get more popular. There's other competitors out there We're like we're virtualizing so many things you have a whole generation of ops people that aren't even they don't know How to work in the cloud right there used to like procuring some hardware like setting them up You know with some custom batch script, maybe the using CF engine And then like that things is gonna run and we're never we're gonna pretend like it never like shuts off And if God forbid it dies, then you know, there'll be some horos going on and what happened with the Ruby community, right? Like they're wanting to rails is new people are coming in they want to get this stuff in the cloud And there's no tools for that and then the Ruby community finds out is Ruby's a great DSL And they start writing the tools themselves and all of a sudden you get things like cap astronomy for deployments You get puppet and chef for as a much better replacement to CF engine for configuring servers You have multiple sort of ways of running multiple Ruby versions and development and even God forbid production if you wanted to You have awesome sort of developer tools like vagrant, which is a Ruby script around virtual box It makes life much easier for us and things like sense you which is a Ruby replacement For Nagios so all these great. So Ruby didn't create the DevOps movement But certainly what they were doing they found that Ruby wasn't replacing PHP It just actually created this entire generation of DevOps people that have built a bunch of tools that basically everyone in web development community uses or has been influenced by So I was that sort of happened. We kind of see it actually going on now, right? You take a UI developer slap some note on almost and you got a full stack full stack developer like but why is this right? So I was a PHP developer when node came out and I remember looking at node going why Why would you why would anyone want to do this and a bunch of other people who were writing Python or Ruby said the same thing? But what's going on here and note is a new technology, right? Like nothing that node sort of brings to the tables brand new you've been able to render JavaScript on the server For years using Rhino. We've been able to do a sync IO Using LibEv I think was that what it was before LibEv was created and used in Node So there's no there's no new technology here. It's been around at least a decade Why all of a sudden like is this such a powerful movement like what's what's going on here? And you just sort of think about like what's eating the desktop, right? And if you're a UI developer your requirements being given to your like hey make the web feel like it's native What fast and everything dynamic? I don't want to reload the web page Just make everything like single-page apps age absolute everything. So these UI developers are writing HTML and CSS and JavaScript They're going to the Ruby person and they're saying hey I need you to give me like a lot of this like event driven sort of interaction You do a lot of this stuff for me, and they're going like what are you talking about? I know like rest That's all I do and they're like no I want web sockets. I want you know all kinds of like you know nice interactions and There's this there's this friction, right? So with node all of a sudden you took a UI developer who was fighting with thirst with their back and engineer And you're like hey, here's all the tools you can just do the whole job yourself You already know job a script. Here you go. So now all of a sudden you have a full stack developer. It's happening right now And for a long time the server side people just didn't get it You saw blog posts like no JS is a cancer people putting up blog posts like hey Fibonacci runs really slow and node and as we all know now it's more of a mature ecosystem like it's really powerful It's really good it complements a lot of what people are doing out there And it didn't so no one lost their jobs doing PHP or Ruby or Python development But what you have is a whole generation of people who are only working in the browser now working on the server side as well So I think the same thing is gonna happen with trust right you're gonna actually have a week and have web developers And you're gonna sort of convert them into systems programmers And that's not to say you're gonna stop being a web developer You might actually just do both like you're just gonna sort of you know let this sort of enhance your tool set And so it but and why do I think this is needed so as we all know like ten years ago We were building monolithic applications right One repository a web server Maybe your runtime behind that and if you want to scale that out all you kept doing was horizontally scaling right spinning up More VMs are procuring more hardware and that's unless you like the point one percent of the industry That's basically how you scale Go more databases behind and read clusters right now even sort of the average sort of Engineering shop has micro services and all these different parts of their application And if you start going through like some of like the concrete examples of each of these you're gonna find out like None of them were actually written in the languages web developers know Right yet. We're highly reliant on them. So if we're talking about application caching like memcash or redis both written in C HTTP caching layers varnishing squid Job cues I think rescue might be like the one example here if you're in the Ruby community We have like a Ruby sort of implementation of a good job cue that you can use if you're in Python or PHP like no Using something using like gearmen or rabbit Relational SQL databases these are all written in CRC plus plus Search databases the next other exception here. Those are written in Java Though any ops person hates this because you know when you're running high workloads and the GC pauses on you at your most critical time You're getting really mad and see fight with it non-stop and every ops person wishes that these are written in CRC plus plus Load balancers and VM in containers like we're all like VM in dock rising everything under the Sun right now, right again All written in a non web language. So we're highly dependent on this entire ecosystem of Software that web developers have basically very little access to And what you basically has of this massive knowledge gap, right? Well, it makes it worse that all this stuff is open source So there's really no one getting paid to do this stuff Which means you have very very few people actually working on this this is open source software they actually have the know-how because it's CRC plus plus and This is bad for a few reasons one like we've seen burnout in the community Also, we've seen that a lot of these maintainers don't have time for things like you know important ancillary tasks like Documentation or putting out proper releases and things like that Also hinders the state of the art when you don't have people sort of like fresh people getting into a certain area Sort of a couple winners or merge and then things sort of stagnate, right? We saw this with the web browser for a while where the where web developers weren't actually involved in the web standards You had a bunch of C and C plus plus people that were developing browsers Sort of you know figuring out how API's and the web worked and when they would come out all the Like what are you doing like that makes that does not matter use case at all only recently do people In the web community have they started joining TC 39 we've been getting things like ES6 and ES7 like that wasn't an accident folks Like it really took web developers getting involved. I think Likewise, we're missing core use cases in the web community because we're not getting involved to solve Parts of our web stack for ourselves because we know best And I and there's a clear example as I write PHP during the day all the time PDO is the then sort of the Database adapter abstraction in PHP means PHP data objects. The person who wrote that was is a C programmer he doesn't write PHP and The way he wrote it in the constructor of PDO when you do new PDO with people normally do in the bootstrap That actually makes the network connection to the database like no developer web developer wants to make a Connection to the database in the boot ship of their code unless they're explicitly saying it and you have to like kind of work Around this as a php develop like everyone has to do it's crazy, right? And it's because I think you know Wes wrote a great library, but he missed a core use case because he wasn't actually involved in the php community So I'll go ahead. I'm sorry. Yeah, I think The For the video saying I'm overselling it sort of laying out sort of like the background of why I think web developers are getting involved in Russ now and We're gonna go through certainly a lot of code examples, but I thought sort of the history was important to sort of present So some barriers entry for web developers to get involved in systems programming, right? Why haven't people and this is why I think I want to set this up like Russ is fundamentally different here, right? Web developers aren't getting involved in C and C++ because it's unapproachable in my definition, right? There's a problem with memory management when you can sort of compile your C program and it compiles fine And then you put it into production and all of a sudden it's seg faults on you at the most critical time Like you're not going back like that's a super painful experience And so if you're a web developer trying to get something done and try to get this critical task out But that's not making you look good C or C++ sort of provide that foot gun for you And you can't sort of rely on GC if you're doing systems level stuff as we discussed and I think a lot of web developers Don't want to write C or C++ because it's not an expressive language, right? When we write Ruby, we like how expressive it is when we write Python We like not having to do semi-colons and we shouldn't have to necessarily give all that up We want to write systems programming And a lot of times we don't need types and we've kind of going to custom not having types So Russ can kind of solve a bunch of that stuff, right? So these are the three tenets of rust and we're going to go through and talk about how they allow you to do memory safety Give you fine-grained control, which is your cake and sort of not let you suck fault eat it too We're going to show I'm not getting too much into concurrency because it's hard to show in examples But we will get into a lot of abstraction without overhead but I do want to make sure everyone understands like I think too is super important here too because as we know right now we're taking a Single piece of hardware like a server and we're breaking that down into virtual machines and Docker containers I think at some point pretty soon here in the future. We're going to see a lot more use case or something like that patching That's what we're taking actually Multiple machines trying to make them work in concert together and right now trying to do that if you have Architecture that's horizontally scaling you sort of know that that actually is a lot of time and effort to make sure those things are Coordinated correctly so having concurrency without data races is super important. I think for a sort of the future where a lot of this system stuff's going All right, so let's get into some of code examples Rust right out the gate has basically this concept of ownership which allows us to sort of have Memory safety without garbage collection. We're going to go into some examples of how ownership works So I'm going to start walking you through rust syntax with the examples if any of the syntax doesn't make sense You know, let me know But in this example we're defining a function take that has a single variable V of type vector of sing of signed 32 bit integers And then below that function. We're going to actually define a variable V That is a vector of 1 2 and 3 we'll pass that variable V into our take function And then we're going to print out below that the first element of V and any other language that we've probably been exposed to This is perfectly fine perfectly sane and perfectly normal. It'll compile it'll work. It'll run in rust This is an error And all it comes down to ownership Right. We are actually passed our V into the function take and once we did that We have given up control in that scope of using that value V again. I'll show you the code again So line three once we pass V into take We are no longer allowed below to use V or access a D anymore and this fundamentally I think there's way there's references and other stuff that rust introduces here But this fundamentally is how rust gives you memory safety Without sort of the garbage collection piece here It basically ensures at compile time that you're not doing anything that would basically cause problems, right? And this is really nice because it solves a whole class of problems that C or C++ don't solve for you You don't have segmentation faults anymore Because you're not basically allowed to sort of use after free essentially, right or even use before you've allocated it The compiler is sort of using this rule called like the barb-checker It's using this barb-checker is going through and basically making sure at compile time You're not doing anything that would basically blow up your program at runtime And you also don't have memory leaks anymore because rust is sort of handling that the freeing app after it's allocating It's also handling when it's supposed to free. She got to worry about this whole notion of I need to have Functions that are dedicated allocating memory and then making sure I'm only freeing that memory and then nothing else is sort of accessing it, right? And if you've written C C++ before you know, this is sort of a chore to do and rust does it for you for free The problem is when you sort of start out with rust you're going to start fighting the program because As you saw like that was a perfectly valid program that we wrote and we all know that like that's how we write code And rust doesn't let you do that and so that you get nearest when people start writing rust They normally start fighting the barb-checker and I'm like, no, that's perfectly that's perfectly normal I do that in like this other language. Why won't you let me do that and really what's going on here is the the sort of The barb-checker isn't doing anything actually you shouldn't be doing if you're actually like a grizzled veteran C++ developer You basically have been turned like internalized like these conventions of the barb-checker and what you just sort of know Like it's like tribal knowledge to you You go through you read like some like the best C++ development books will actually like outline these steps Like makes you do all these things and what rust did is basically codified back into a compiler So what rust is, you know, when you're fighting the barb-checker Kind of take heart and that you're kind of making an investment and that the compiler is literally teaching you Painfully at times because it doesn't care how frustrated you are and it doesn't actually go on vacation But it's teaching you to become a systems programmer And you could actually take those same rules and apply it to other systems languages and be just as successful One thing actually like about this too is you could contribute back with confidence, right? How many people have sort of wanted to put a patch into a large program either where they work or even an open source and Kind of felt a little uncomfortable about actually pushing that pull request button, right? Because even like the test pass like you're like, I don't know like it might blow up at one time I might have missed something right and you know, it's kind of scary Especially if you're new to that community or your new developer Even if you're seasoned you look like a like a knucklehead if you just sort of miss some obvious thing, right? And the great thing about the barb-checker even though you're gonna fight it at first You're gonna get to the point where like when that program compiles you pretty much know that is it's gonna work And if it does blow up, it's not gonna be for some obvious thing. It's gonna be for something kind of crazy And so like I myself have Contributed to rust core and I'm not like that good of a programmer and but once the thing compiled I just felt like I can push I can push the button here I can actually send this patch over the wire because like the compiler told me I'm a good systems programmer And for this particular piece of code Cool so great so now we've you know, this is why Russ is so compelling in terms of Writing save code, but you know, I did talk about like we don't want to give up some of those aspects of scripting languages that we like Right, so let's get into some of these things that Russ does that we don't look like we're actually writing so much sort of C or C++ code so Russ spends a lot of time on ergonomics and We do think writing the code should be as enjoyable as writing it in a systems like scripted language And we want the compiler of use the compiler to figure out all this borrow-checking stuff Right all this ownership stuff, and we also use the compiler to figure out all this sort of boilerplate stuff that you Have might do in other languages that have strong types and we sort of meant let the compiler for it Like you know, I can't get an example to come on a closed out But here's like in Java right get to define the types on both sides of the assignment It's like why I just defined it on one side just figured out on the other side and what we end up doing right is instead We we produce IDEs that basically fill in all the boilerplate for us, right? And instead of using IDEs Russ, so let's just stick into the compiler So here's an example simple example. We're gonna find some variable foo. That's gonna be a typed Sign 32 bit integer and we're gonna sign it to the variable five and as humans We can all reason about very quickly like that's a number. I don't need to specify the type So neither does the compiler and this looks like a lot of languages that are like scripting languages, right? But foo equals five no type necessary That's great for like type Like assigning like variables of types. We also can go one step further the compiler can even figure out In certain cases how to even default the return type So here we get a little more complex. This is how Russ defines a closure so we're gonna find some variable f and it's going to Define a lambda function that takes in a single variable x of type sign 32 bit integer And it's gonna return a sign 32 bit integer and it's gonna return x and this is an example of the identity function If anyone's curious and this is how you write a closure in rust or a lambda function in rust It's pretty pretty terse. We can even get better That is how you can write that same function in rust and again that looks a lot like a scripting language So you can go all through a lot of your rust code and not actually worry about specifying the type and That's nice because Yeah So I'm gonna say that the reaction by actually writing it So not only is it nice not have to write the type in certain cases. This is actually more expressive even to To your code because you can use the same function to actually Allow multiple types come through and the compiler will know that so it's even actually better like this is encouraged, right? Late new as programmers were sort of taught like laziness is a good thing and here it's showing up in the positive And we're going to go a little more complicated example These apostrophe a things are called lifetimes. And so what we're doing here is we're defining a function That's very similar to our identity function called silly And I'm introducing this concept. I'm introducing a lot of concepts here, but ampersand means reference And that lowercase str is a a string type one of the string types in rust So we're passing in some Variable s of type string and pass as a reference and pass in that reference back out And we see like these apostrophe a's All of rust's functions require you to sort of define these things called lifetimes And that's how it actually figures out how long things like references should live for it's all really complicated to be like, you know Some programming language theorists actually figure out a lot of times when certain lifetimes can be used But the great thing about rust again, we're using the compiler like 90% of your code Does not need the lifetimes at all the the compiler infers it This is actually really nice because we get all the power of lifetimes without having to worry about it So if you're a new developer and you're trying to do something in rust You don't want to have to start thinking about lifetime to try to just get something done, right? And so the default here is lifetimes lifetimes are there under the hood But they're just sort of like inferred for you and when you want to actually turn on like the advanced switch Like you can switch that advanced switch on and start telling rust No, no, I know exactly what i'm doing here are my lifetimes and be really explicit about what you want to do So you get all the control with sort of none of the penalty All right at this point we sort of kind of kept it pretty introductory and I've been introducing a lot of things that are probably Pretty basic we're going to sort of step up a notch here to a little more theoretical stuff that's going on Anyone has any questions? Please stop me and ask So um optional correctness Yes, go ahead So the question is is the bar check a reference counter? No, it's not the bar checker is a program that That there's by default. There's no sort of reference counting going on We're we're literally at the middle level where like c is It's it's just it's it's there's a couple phases in the rust compiler This is the first phase that it goes through and just sort of make sure the semantics of your of your code Aren't violating sort of any of these constraints, but so it's not a runtime thing. It's done at compilation time lifetimes. Yeah, I think about lifetimes as basically a um It's less of a scope. It's almost like Typifying sort of how long Reference to variables should live Lifetimes are one of those things I introduced it. It's going to come up if you're getting into rust You don't need to actually worry about them hardly at all But you kind of want to know what they are in case something in case you run into something when you're using a reference I would say most people can I've written rust and they'll go months without ever needing to put one lifetime in place So they're there be aware of them. Don't be scared of them All right optional correctness One of the things I love about rust is it sort of takes this nice balance of theory And applying that theory to actually real world problems And as we know the real world is not always sort of on our terms And so, um, you know, I like like a language like Haskell. It's great But you may have to jump through a lot of hoops sometimes to make your Programs work and I think rush tracks is really nice balance. I think we can sort of Great example this balance is in rust. You can do really return types, right? So we've all seen this type of code. We're going to let r equal the value of some function Return type of term value of some function if r is an error, we're going to return early return false And then we'll go down our happy path code, right? We've all write that when we write like our our normal code And we write that in rust too But rust also has this monadic type, right? We're going full burrito now This monadic type option that can wrap a type t So any type t it can wrap this and if you're not familiar with option It's like the either type in Haskell Option can basically say look if there's something here of type t or there's nothing here, right? So it kind of maintains these two states and if you're going to go like to programming language theory sort of side of things This is a monadic type and monadic types are not allowed to return early Rust is like whatever you can return early. So what we have here is an example Let variable flu equals some string bar and we're going to use the find function on foo to see to find the character b Right and this match statement in rust. It's kind of like a switch statement, right? But it makes sure like you sort of cover all the different use cases So as I said before option is either there's something there or there's nothing there So if there's something there some character see there Then we'll just return c back to our variable. So c, you know, is the is where that position is Um, and we're good And that's what this code is going to do, right? We can have a different example where our string is no And b's not there and we're going to go into the this other branch of code This other harm of our match statement where it's not and we're going to call this this Macro panic which basically means like fail air Bail out of your code stop running the code. It basically return early type So, you know, when people sort of uh, if you're a programming language theorist, you see option like I love monads But rust is like look you can return early too. So you kind of get all the power of monads Like sort of all this simplicity of sort of returning early Which is just easier, right? You don't have to like sort of contorting your code to say like, okay I get only one return type at the very bottom of my function scope You're trying to figure out how you can break your functions even smaller functions to deal with sort of the different um If else, you know statements that need to happen Um, it really makes things much more approachable And this this pattern right here is so common in rust that they actually made it even easier to do and they have this method called unwrap So this is the same thing as before That basically unwrap will say look it if you found what you were finding I'm just going to return it to you and if you didn't I'm just going to blow up the code And I feel like man, why would rust put that in there? I thought you said this was supposed to be like memory safety and like Really good systems programming stuff. Well, it's true But if you're starting out in rust or even starting out trying to solve a problem You don't want to you're not worrying about correctness, right? You're trying to hack your way from like start to finish and get something compiled and something generally working to see What you're at, right? We don't normally explore new spaces by sort of by doing tdd first, right? Tdds after you've already sort of mastered your ruby or your php or python Tdds when you're sort of just trying to you know, you're on rails if you're starting out rust or sort of exploring some unknown problem Where you're trying to figure out like some weird protocol and you're twiddling bits You don't want to begin with tdd, right? No, you don't want to be doing the really correctness either you want to turn that off and so you can use unwrap So a lot of time that people do is they'll write their code and they'll just do their first pass and everything goes be unwrap unwrap unwrap It's fine if it blows up. It's just it's not production ready yet That's okay. And you can go back later then and actually say I'm gonna now actually handle like errors properly You know log them you're gonna bubble them up. You can do all these different things. So it's really nice Again a pragmatic approach And really I think it goes down to rust sort of says like the programmer knows best Like we're gonna sort of put all these defaults in place and we're gonna sort of set this really nice This is programming language up But the programmer knows best and we'll sort of allow them to sort of override things when they want to So much to the point that rust also has this notion of unsafe Where I sort of told you before you had to satisfy the baro checker if you wrap coded unsafe Baro checker says I trust you So here's an example where we're actually creating the sparkle heart emoji out of a vector of Bites and we're saying like look at rust trust us. These are valid utfa characters If they're not Then that's on us and what this is sort of nice is like it again It allows us if we're trying to really take A theory and apply it to the real world. Sometimes the real world is not in our terms. We need to do something crazy, right? Um, in this case, it's sort of we're sort of demarcating like I need you to trust me right here rust and also to all the other developers out there like hey I need you to trust me here rust is it doesn't have my back here. I'm saying I got this So let's talk some about zero cost abstractions what I sometimes call mullet driven development Uh, you're gonna get all the sort of the expressiveness or actually all of it a lot of the expressiveness of ruby So you get sort of ruby in the front when you're writing your code But you don't take any of the performance costs really get like c in the back, right? Um, this is great. So let's go through some examples here Right here. We are defining some variable n and then taking a range of numbers one to ten We're first mapping them. So we're multiplying all those numbers by two then calling filter on them So any number greater than five we're dropping and then we're adding those all up with our fold Function here with our with an accumulator This looks very similar to something like right like in python or ruby, right? And this is like if you're like, you know, we'll cutting edge ruby is at this point like you're like no for loops ever I map everything. It's great. Um, you can sort of do the same thing in rust But if we wrote this in ruby, right like first we're going to um, basically Loop over every single one of those numbers one to ten once from app And then we're going to loop over the numbers again for filter And then we're going to loop over the numbers again for fold And that's going to be basically three n times Now if you write ruby again, it doesn't matter because that's not what you're trying You're you're you want expressiveness. You're not trying to optimize your code for performance You want expressiveness maintainability readability and we take that hit. We don't care But if you're writing a systems Program, you do care. So it actually this is dollar rust code what rust is going to do under the hood Is take map filter and fold instead of doing those three iterations It's actually you know, create like a single loop and actually do all that work in a single path through of 10 iterations It'll actually inline map filter and fold and sort of do that in sort of one one shot And you know, so you don't need to worry about it Like if you want to write your code expressive and make it really clear what's going on You don't have to go back later when you when when you want to optimize it and sort of make it almost unreadable Has anyone ever looked at the open SSL code layers before? Like you wonder why there are so many bugs in that thing, right? They're trying to make this thing fast and when they do that it becomes almost unintelligible What's going on here? We can make it really clear what's going on yet still get all the performance out of it Could you add more? The question is can you add sort of methods like this as a user of the language? So the nice thing about rust is rust is written in rust and these are just iterators So you can write your own iterator methods and and sort of do this yourself and you and people do this all the time Another question Yeah, sure The question was why does filter take a reference instead of the actual type? The reason why filter takes a reference is it doesn't actually need to own the variable to fix it's just basically Um Needs to sort of use the variable to check something real quick Unlike map map might actually mutate the variable filter is just sort of peering into it So you don't actually take the variable. You just take a reference to the variable And that's common like in the generic filter method, right? So these are all like immutable things So you can make map take a reference if you really wanted to other questions And then we can even go one step further Where rust really does have first class functions Like a JavaScript Um type language, right? I wrote some code. I created a simple Ripple read evaluate print loop for rust and this is what my main function look like Again systems language over our read evaluate and print are all functions And again the compiler using LLVM compiles is down and I'm we're taking no performance hit And we're getting sort of all the the monadic stuff going on here with the admins We have all the expressiveness like it's very easy to see what I'm doing here And all the speed of c Let's go one step further with generics If I define a function here first And if you're familiar with like java or anything like that We're defining a generic type a and a generic type e And creating a tuple out of those values And we're basically just saying like it's going to return that type a in the body of our function We're saying for that tuple just return the first one. So that's what the function is doing, right? Very simple Most languages have to do this at runtime, right? Like if you do this in java um Java is actually figuring out at runtime like how to actually like Figure out all the typing stuff and and you can run into some problems there where it can actually not figure it out um So for rust we can do the same thing like uh, you can't with any other generics We pass in integers. You can pass some strings um And uh, and you know, even when we do this is something like ruby or php or python It's doing this at runtime. We just never see it. It's a hidden cost, right? Actually, what rust does is it has this concept of monomorphization Long word don't have to remember it But essentially what this does is the compiler creates a specialized function at compile time for First sort of each type, right? So if we're passing in uh, um If we're passing in an integer The compiler goes oh, I see you're passing integers in your program I'm going to go and create a specialized function for integers for passing in a string It's like I could see you're passing in strings as well. I'm going to create a specialized function for strings I can look basically like this like you could do this yourself too, right? You can write specialized functions rust does this for you for free So you have to act you can write really generic code We saw with our closure example earlier and under the hood It's creating these specialized functions for you that you don't have to manage It's just like I got this for you So again, all the expressiveness of sort of a higher order language with none of the sort of the performance cost Sort of question Uh, and this the question was could you have a string and a numeric? I am defining two discrete generic types. So you could Um, I didn't show that example, but yes, you could kind of come do any sort of generic stuff typing that you want Great, and then let's move into, um Another topic here. So systems programming We might think if you want to write become a systems program or write system assistance program You got to write the entire thing In sort of that language, right? It's all have to be in c or all rust or all c plus plus Actually, I would argue systems programming doesn't sort of the definition doesn't actually require that. Um What we can actually do still is write the bulk of our program in something like ruby And then once like we're done writing that program we can actually go back things like is there any like really Like slow bottlenecks that are not IO bound vcpul bottlenecks and if there are we can actually drop rust in Um to actually solve that problem I actually did this there's a library out there called selecta Um, which was written by gary Bernhardt, which is really nice sort of little command line utility That allows you to quickly search your file system for things It's really nicely written and had like a full tty and all this support But there was this one hotspot in the code where there's this really efficient search algorithm But because it was ruby, it just could never run fast and it goes always like the biggest bottleneck in the code I actually just took that library rewrote the search algorithm in rust And um put it back in uh compiled it into the overall program in ruby So ruby could just call my rust code and we can do this because um rust treats See as sort of the common language across the entire ecosystem, which is basically true, right? And rust interrupts the c really well I'm not showing an example here because it's it's it's a it's a fair amount of code to um get it going but There are a lot of examples of uh this on the web On my blog post I have one for php again. I've written one for ruby. It's fairly approachable There's some obviously there's like some boilerplate work to sort of tie into those c um Interfaces that are sort of defined But rust makes again makes it pretty easy and because you have all that fine-grained control rust Rust isn't like a gc language, right? If you try to do this as something like go which goes a great language, but go as a gc in it And if you try to match goes gc up to ruby's gc, what do you think is going to happen? like not good things um Because rust doesn't have a gc just like c or c plus plus um you can easily tie into this even note so what recently came out to was an example from um someone uh at the Remember his name now At mazill actually put a whole block because about how you could actually tie Rust into the no j s runtime, which is written in c plus plus a little bit harder than c Let's see here. So I went over a lot of concepts It's really hard to sort of go through some of the code examples here But uh my these there's a lot of sort of ways you can sort of start digging into rust Oh, there's actually a rust book out yet, but the documentation on the website Uh is great Steve Plabnik has done a great job. He's actually put up mazill and does that full time But actually even better we learn rust is to come to the rust of los angeles meetup I actually run that meetup And we do things like last month We took there's a new program out there called a stock fighter anyone's heard of stock fighter before And we it's basically A game where you write an automated stock trading Um Program and it's it's pretty real not a hundred percent real. It's pretty real and uh, Believe it or not now I've written a rust sdk for it. So we're writing a rust sdk And really I think the hardest thing about you know with any of this stuff is getting started, right? Rust lowers that barrier to entry to get started a lot But there's always sort of something that doesn't make sense and so if you're with a bunch of other people Those questions get answered really quickly and you actually feel like you're making a lot of progress There's the website on there And again reach out to me if you have any questions That link at the bottom is a link to all my blog posts on rusts and there's actually a number of them on Strings, so if you do start writing rust and strings are frustrating you Go ahead and check those out to make things a lot more clear But feel free to hit me up on twitter or you'd have or anything if you have questions as well And with that I'll open the floor Yeah, you can there's actually the question was can can you generate a rust program for something like Arduino? Rust recently in 1.6 was just came out Actually a stable support for not including even the standard library of rust only just like the bare of the core Components which allows you to compile it for things like the Raspberry Pi or Arduino And basically the footprints very light you get you get just the bare bones and then It is I realize that um you still can do for the Arduino you Rust the rust compiler has compiler targets And you have fine great control over the different compiler flags that you can send and make and you can sort of compile it Anything you compile uh c2 The the question was how small can the is the rust binary? Yeah, so rust hello world is a little bit larger than you might think Because again, it's doing a lot of that sort of specialization under the hood Again, if you need to optimize something you can do it unsafe. I don't remember off the top of my head But I know it's it's it's fairly it's it's a fairly large hello world. They have not tried to optimize for the hello world example I know um some people have been interested in that but it's kind of a red herring I think once you get into something that's beyond just a simple program It sort of gets amortized out and and you're basically um sort of on par with c or c++ Don't quote me on that. You you could certainly ask on the rust users um forum And they would be able to give you some direct examples. I think there's been some talk about that So um, I'm not into that space as much so I can't I'm sorry. I can't answer like definitively Yeah, but certainly it's it's a of a lot of interest to the rust community embedded programming stuff like that other questions Joel and then There is a rust um libraries or user libraries and rust are called crates There is a rust crate that's starting to replace parts of open ssl I think you know rust is 36 weeks stable They went through eight years of basically um prototyping to solve that main core use case that of servo So we've been 36 weeks in like in like production mode like a 1.0 release and we're 1.6 now And I think really the community is sort of looking at things that form like that a 10 year frame of reference So I think we'll see more adoption of those things as they come But um sort of things are kind of going slow as we're sort of figuring out things like Even things like higher kind of types I think as we sort of figure out more and more things within the community You can see more adoption and see things like I can open ssl solve port to rust or other things like that I myself Do a lot of work with a library called mia, which is a replacement for Live UV so it uses things like equal or kq if you're on vSD But it's a complete rust replacement for live UV that is and then I think dropbox just Put out that's what they use to sort of create their Hard drive mesh and to figure out a load balance all those different files across your system So it's coming and some people are really interested in doing that because it's just it's nice to write it So, yeah Your question So the question was can we talk about concurrency control and rust? Yeah, it's I would have to dedicate like 10 slides. I think sort of to walk through Just how that sort of works under the hood. Um, there are some examples up, but essentially rust is using the borrower checker and You can sort of reason about how you're allowing Threaded computing to share memory via the borrower checker And so it does that all at compile time. So it's really it's using that those same semantics It's sort of enforced that you're not going to step on a piece of memory So once one thread takes ownership for piece of data the other thread just can't you don't need semaphores or new texts at that point, right? There are and then you can actually use a lot of other semantics that like when things get freed up Or out of scope to pass back ownership upstream to a different thread So like there's a really good way you can like do that with channels, for example, it makes really easy So the question was is that better than go so go? You just focus more like on their like go routines, right? And they do have channels and stuff. Um, I believe the last time I checked the go Go Routines actually leak a little bit of memory in order to do the a lot of their safety guarantees So again goes goes a great language. I think it's you know, it has a lot of use cases I think to like write to like replace A lot of java stuff for like web services. It's like kind of lighter weight than java I think that's where it really shines and again, you don't really care about if it's a little bit of leakage there, right? but um There are coroutines and rust you could do the same thing, but um, they just sort of have different strategies again Go has the garbage collector other questions Here and then we'll go back to you What is the next ruster? Yes, the question is when is the next rust meetup and where do we meet the next all the meetings are usually in the middle of the month so um It's whatever that Wednesday in the middle of the month is and we meet and we meet in The notion rack Slash hope look production studios in culver city And if you go the website has like a Huge thing of direction so you can get there. Yeah, check out the website has all that information. Hope to see you there question Yeah, so competition is good. Um, I actually don't think it's really gonna impede it too much You know, we've even seen like in c++ a lot of stuff that we've been doing in rust The c++ community has been trying to do To like take on as well. There's actually now some sort of certain pointer types that sort of have some kind of Ownership and properties that rust has been sort of messing around, right? If they're already like things like own pointers and c++, I think um Swift is going to sort of make it more again make things more accessible. You're going to get a lot of people who were writing iOS apps Trying to write actual like, you know server side stuff like in the usx ecosystem So I think actually it might only speed up adoption of something like rust They have a lot of similarities, but they also have a number of differences too Um that the swift community said like we're not we're not trying to compete at that level that or that sort of some of that Compete some of the areas where rust is trying to compete another question so the question is does if The question is if the difficulty of functional language semantics would be a blocker for the adoption of rust So rust actually isn't rust actually is an expressive language not a functional language So it actually can do a lot of functional things Um that you find other languages But every single thing that you do in rust is an expression I mean by that is you can actually assign a variable to the result of an if statement Um Which is actually after a while quite natural So you don't you can write your code in rust and the most basic imperative form that possible Just straight like like almost in like in that classic c stop, right? Which is a bunch of functions and that you just sort of cobble all together make a program out of it So again a lot of the stuff that we talked about is opt in you you don't have to take advantage of like map and filter And fold if you don't want to You can actually just write just normal functions and it'll work just just the same So I actually don't think that's too big of a deal. I think it's really more of an upsell that it can do those things cleaning most people Sort of want sort of a higher order feel to the languages they're working in now Question the question was does rust have exception handling? So rust does not have exceptions It does have a macro called try Which at first glance might look like like though and I like deal with exceptions That's just bubbling up things like options or result types But the the language designers make and made an explicit decision not to sort of add exceptions into the language Panic and then though the things look like exceptions as well like they'll sort of quickly abort the program But yeah, I don't think there's any plans for exceptions in the language as well That's sort of a highly debated topic, but That was the decision Any other questions? All right. Well, thank you so much for listening to talk. I hope to see you at the meetup Like At Oh Oh