[Script Info] Title: [Events] Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text Dialogue: 0,9:59:59.99,9:59:59.99,Default,,0000,0000,0000,,I'm excited to be in Germany and Berlin for the first time.\NI showed up a few days ago and I've had some time to get into some of the food, drink and history.\NIf anyone has a recommendation of places to go -\NI've got another week here, so catch me in the hallway track.\NI'd love to hear what you suggest.\NI'm also flying out to Switzerland.\NSwitzerland has a lot of things relevant to my interest.\NWatches, chocolate and I'm sure it's complete he undeniable that their flag is a bug plus. Dialogue: 0,9:59:59.99,9:59:59.99,Default,,0000,0000,0000,,[Laughter] Dialogue: 0,9:59:59.99,9:59:59.99,Default,,0000,0000,0000,,Now we're here to talk about learning to love the unfamiliar.\NAnd what this is, is, it's kind of journey I had while learning Rust.\NIt's stuff I ran into when I was unfamiliar with compiled languages.\NIt's use annuals and concept that is were unfamiliar to me, coming from languages like Ruby or JavaScript or Python.\NIn the next 30 minutes, what I want to do is I want to help introduce people who are new to language\Nto these concepts.\NThat way when, they hit them for the first time, it's not going to be as big of a hurdle to get over.\NI want to help you kind of ease into that a little bit nicer.\NMy name is Brian Pearce.\NThank you for being with me this morning. Dialogue: 0,9:59:59.99,9:59:59.99,Default,,0000,0000,0000,, Dialogue: 0,9:59:59.99,9:59:59.99,Default,,0000,0000,0000,,According to the Rust survey 2016, 1 in 4 people being believe that Rust has a steep learning curve.\NI don't think there are too many people that are going to argue that. Dialogue: 0,9:59:59.99,9:59:59.99,Default,,0000,0000,0000,,There are a lot of new concepts, depending on where you're coming from.\NConcepts that will just be new to you.\NSubmitted that it takes three weeks to get over this hump.\NWith that if mind, let's talk a little about frustration.\NWithin the fist three weeks, there are things that can be barriers for people.\NPoor documentation or maybe the community access isn't as open or friendly as Rust's community actually is.\NWhen you hit some of these bumps, you just might not know what to do about it,\Ndon't know where to go, who to turn to.\NYou're potentially trying to achieve something you don't know how to do in another language, and you just can't work it out here.\NAnd that gets incredibly frustrating.\NWhen you first decide to pick up a new language, have you something in mind.\NI want to build this or I want to do this thing I know how to do in another language.\NHave you a single purpose you want to accomplish, when you can't accomplish it it's frustrating. Dialogue: 0,9:59:59.99,9:59:59.99,Default,,0000,0000,0000,, Dialogue: 0,9:59:59.99,9:59:59.99,Default,,0000,0000,0000,,So let's take a look at an example.\NWhen I first started learning Rust want I wanted to port a little bit of code I had somewhere else, into Rust.\NSo we're anything to look at a little bit of Python code.\NSo what have you here are two functions that is read in a time file and process the lines of that file, send it to a process func, check to see if the line takes a pon.\NIf it contains a PON, you should tell a friend.\NHighlighted, if highlighted, the key points in these two functions, the first highlighted lines we're going to opt file.\NSecond highlighted line, iterates over the lines of that file.\NCross check see if it's a pun.\NTell a friend.\NI want to port this over to Rust.\NSo let's look at what that looks like.\NSo it's not too different.\NAgain, the key areas are highlighted and they're probably much exactly the same.\NYou have a file open call want you have a loop to iterate over the lines of the file and pass throughout process func and check to see if it's a pun and again, tell a friend.\NKey points highlighted other it's very similar.\NIt's almost what you might expect if you're coming from another language and trying to put something together.\NThe thing I begin to notice when I started writing more Rust were these.\NThe unwrap calls.\NI didn't know what they were for.\NI knew the compiler told them, they had to get the values that I wanted to get.\NI didn't know if it was boilerplate.\NI didn't know what was going on. Dialogue: 0,9:59:59.99,9:59:59.99,Default,,0000,0000,0000,,And I'd look at this time a file after a couple hours work and be like what, is this?\NI don't understand understand it.\NMy code would have looked a lot nicer if these weren't here and that was frustrated. Dialogue: 0,9:59:59.99,9:59:59.99,Default,,0000,0000,0000,, Dialogue: 0,9:59:59.99,9:59:59.99,Default,,0000,0000,0000,,I want to take a bit of a dive into why that is there, why we end up seeing it in a lot of places.\NSo Rust doesn't claim to have maybes but it does have maybe types.\NMaybe is a pattern and Rust implements it via option and result.\NSo we're going to look at option first.\NAnd how option works, it's an enum that contains some or none.\NIt contains something or nothing.\NAnd when I first read that, that didn't seem incredibly helpful.\NI'm callation function, I thought every time I call a finger, I'm going to get something, like the value I expected or a runtime function.\NBut I never expected to get nothing so I never really understand this as a concept. Dialogue: 0,9:59:59.99,9:59:59.99,Default,,0000,0000,0000,, Dialogue: 0,9:59:59.99,9:59:59.99,Default,,0000,0000,0000,,I don't want to just try to entertain T.\NI want to talk about where it should be used and why.\Nso on the screen, I've got a string.\NThe string contains the character 7 and we're going to convert to do an integer with a Ruby Y.\NWe get a 7.\NPretty much way expect.\NWhat happens if we do it with an Emoji.\NIf I call 2Ion an Emoji in Ruby, does anyone have any clue what's going to happen?\NA nil in anyone else?\N0 zero.\NException.\NYou get 0.\NWhich is not what I was expecting.\NLooked like a credit card number 2I.\NYou would get what is represented as a credit card number.\NA malformed what, do you think you're going to get this time?\N4,520, which again, is just not what I was expecting.\NSo now, let's look at that in Rust.\NSo we have a character, 7.\NAgain, the room has a lot people who have been doing Rust for a long time.\NWhat are we going to get here?\NWe're going to get a sum of seven.\NIf we do it with know Emoji.\NWhat do you think we're going to get here?\NPerfect.\NSo we're going to get a none.\NSame thing with credit card number.\NGet a whole number.\NWe're going to settle that number.\NIf we did it with a malformed credit card number, we would get none.\NSo this is interesting.\NIt's interesting because we're getting a different response or result when the conversion doesn't work.\NAs opposed to getting kind of like a mangled conversion that you don't necessarily know what to do with afterwards which is nice, but the thing is this isn't what I was expecting still.\NWhen I call 2 digit on a 7, I was expecting to get a 7.\NWhat I have is a sum of 7.\NAnd that's not a 7.\NI can't do arithmetic with a sump 7.\NDepending on how you understand that sent.\NSee let's take this back around again.\NSo we now know when we call [indiscernible] we get a value of 7.\NTo get an actual value of 7.\NWe have to call [indiscernible].\NThis gives us the 7 value we were originally expecting and this is I didn't had to litter my code with unwrap, just to get value I was expecting to get.\NComing from another language accident I'm not used to some things or nothings.\NI'm used to get the value they expect from my function.\NIf we do it with the Emoji here, we get a pick.\NSo what this means is maybe that we are initially getting, the maybe that we are getting, was there to protect us.\NIf we get unwap, the code is not better than when we were getting values that we weren't expecting, bad one versions and things like that.\NThe unwrap will only work on positive response from the function, assuming that actually got a value I was getting the sum or none.\NWhat I wanted was the value.\NBut now, I can at least see where some or none becomes important.\NWe're going to look at another example just to drive it home. Dialogue: 0,9:59:59.99,9:59:59.99,Default,,0000,0000,0000,, Dialogue: 0,9:59:59.99,9:59:59.99,Default,,0000,0000,0000,,So here, I have a Ruby application.\NIt asks user at the command line to enter a number.\NIt uses the two integer to convert the string into a number.\NChecks to see if the number is a number and if it is, bring the right number.\NIf it's not, we'll say that wasn't a number.\NBut as we have seen in the previous slides, 2Iand Ruby will convert everything to a number.\NWhich means, this is actually dead code.\NThis will never happen.\NYou will always have a number. Dialogue: 0,9:59:59.99,9:59:59.99,Default,,0000,0000,0000,, Dialogue: 0,9:59:59.99,9:59:59.99,Default,,0000,0000,0000,,In Rust I would have originally written it something like this and called it the unwrap.\NBut now, we know that's bad and wean why that's bad.\NAnd wean what we actually get is a maybe request we call a two digit.\NI can begin changing this to something that looks a little more like this. Dialogue: 0,9:59:59.99,9:59:59.99,Default,,0000,0000,0000,,I can take the maybe and now, I can check to see if that maybe is some, did I get the conversion or is the maybe none?\NThat's nice.\NThe maybe is guaranteeing values for us.\NOne thing I want to point out here, this kind of if else condition really isn't idiomatic Rust, in a case like this we would use pattern matching.\NSo I'm going to change that over to pattern matching really quick.\NPattern matching in Rust is exhaustive.\NHave you to count for every case and it's incredibly powerful and used in a lot places in Rust.\NThat's as much I'm going to say on pattern matching.\NI want to point that out in the slide.\NSo in the beginning, when I first started writing Rust.\NWe saw all of those unwraps.\NThey were frustrating because I had to put them there, and I didn't understand why.\NNow they can see why and see what they bring, value wise to me, it's not so frustrated anymore.\NIt's incredibly beneficial, and I can start using that.\NNot just in Rust, but can I begin using that type of pattern in other language that is I write. Dialogue: 0,9:59:59.99,9:59:59.99,Default,,0000,0000,0000,, Dialogue: 0,9:59:59.99,9:59:59.99,Default,,0000,0000,0000,,So we're going to talk a little bit about error handling.\NIn other languages, there's time that is I write code, I just expects something to go long.\NI'm going to call a func and, you know, maybe I'm going to get a time-out from someone's API, or a value that I wasn't expecting.\NIn a lot of the languages I normally write, I use something like a rescue.\NTry and catch.\NRescue crew.\NWe're going to look at those real quick.\NSo this is Java's try and catch.\NIf you're not familiar with Java: How it works, is if you have code that you want to execute, that you're worried about, throwing exceptions, you put it between the try and the catch.\NIf the code throws an exception, then the between the catch and the bottom parentheses, becomes executed.\NIn Ruby, we have begin rescue crew end.\NIt works very similarly.\NWe begin on rescues executed.\NIf something goes wrong, between rescue and executed.\NPython's very similar.\NSo the next session is what do we have in Rust.\NI'm writing code.\NI want to try and catch an exception so I'm going to hit Google and look at stackoverflow.\NWhat am I anything to find?\NI'm going to find the try macro.\NNow, this looks kind of nice.\NIt's small, simple, but approximately 50% of what every, language had.\NWhere do I catch the errors in I don't understand what to do with this.\NSo let's talk about frustration again.\NI know what I need to achieve.\NI know how to achieve it in another language.\NI do a quick Google search.\NThis is what I get and I just don't seat direct comparison.\NIt doesn't immediately make sense what I need to do with this.\NAnd it didn't really begin to make sense until I looked at combining it with the result, maybe, which was the other maybe type I mentioned.\NSo let's take a quick look at result before I come back to how to use try. Dialogue: 0,9:59:59.99,9:59:59.99,Default,,0000,0000,0000,, Dialogue: 0,9:59:59.99,9:59:59.99,Default,,0000,0000,0000,,So much like the option, which returns some or none, the result returns okay.\NIf you get an okay.\NYou know the functions that you called was successful.\NIf you get an error.\NSomething went wrong. Dialogue: 0,9:59:59.99,9:59:59.99,Default,,0000,0000,0000,,We're I know something went wrong when I opt file.\NIf I want to do this in Rust.\NI'm going to look at our readfile function again from Rust and there's a few things here I'm going to change.\NI'm going to give myself a little space.\NGoing to highlight the two places that is I'm going to chairman first.\NFirst one is going to be the method signature.\NSecond one is going to be where I'm actually calling file open.\NMethod silt has a result type.\NAnd result type is a result.\NThat lets, or that says that now something somewhere in this function could potentially go wrong, and I am going to return this result type, maybe.\NSo now, we're going to look at try.\NTry is the second thing.\NTry macro around our file open call.\NAnd what try does for us, is it will execute the file open function and if something goes wrong, tell actually capture that error, put it into an error type or an error result type and then return an error func immediately.\NSo it's going to capture the error.\NIt in the proper too much.\NAnd return func.\NNow that lets the caller of this function decide what to do with it at that point.\NCall a file, something went wrong, then can you begin to pattern match on that, much like we did on the option type.\NAnd can you decide what you want to do with it. Dialogue: 0,9:59:59.99,9:59:59.99,Default,,0000,0000,0000,, Dialogue: 0,9:59:59.99,9:59:59.99,Default,,0000,0000,0000,,You can begin mapping the error.\NSo you can cane it on to the initial call.\NAnd if something goes wrong, can you turn it into something else.\NInstead of just taking the panic that file open gave you, you could add text to it.\NYou could turn it especially a custom type.\N-- Dialogue: 0,9:59:59.99,9:59:59.99,Default,,0000,0000,0000,, Dialogue: 0,9:59:59.99,9:59:59.99,Default,,0000,0000,0000,,Last thing that you need to do\Nnow that we are returning a result, under the assumption that everything actually went well, you need to return an okay.\NAn okay is the other type in the result and okay says that everything successfully went okay.\NLike, your function did exactly what it expected you to, and now, can you return that. Dialogue: 0,9:59:59.99,9:59:59.99,Default,,0000,0000,0000,, Dialogue: 0,9:59:59.99,9:59:59.99,Default,,0000,0000,0000,,So now, we're going to talk a little bit about testing.\NTesting in Rust for me, was kind of like a bit of an up and down, it was a roller coaster.\NComing from languages that have a lot test frameworks and that kind of thing.\NI, again, had expectations, I had how I knew how to do things in other languages, and I wanted to just like, drop into Rust and do exactly those things, and expected it to work.\NAnd unfortunately, it didn't quite work like that.\NSo some of the things for me, as opposed to Ruby or Python or JavaScript, where a lot people write their test in a test directly, Rust lets you write every test inline.\NSo can you define the function in Rust.\NIn whatever file you're writing in.\NAnd immediately use the test macro that's highlighted here and define the test.\NAnd that works.\NIt's again, not necessarily idiomatic Rust a lot of people would prefer to put it in the module.\NAnd change the sim phantom dataics how they organize that file.\NAnd can you do this.\NIt's kind of nice, actually, once I got over the fact that I'm not doing it like I used to, I kind of like this now.\NWhen I'm looking for the test of a particular function that's modifying, I just know it's in that file, as opposed to having to search through the project and finding where those things are.\NRust being one of the first compiled languages I dealt with, the other next thing that I didn't really realize at first was all of the test code, that goes in the files, and my source files is not going to be compiled in the end.\NThe compiler strips a lot of stuff out so you don't have to worry about it.\NSo now that we know, we don't have to write our unit tests inside the test directory, that's great.\NBut Brian, what's the deal with this test directory -- this is where can you write your integration tests.\NAnd how the integration tests work in Rust is every file in your test directory will get compiled into its own crate.\NWhich is why I tried to write my tests, I had a lot hair pulling and didn't really know what was going on there.\NBut how it works is every file in here gets compiled into its own crate.\NSo if you've written a library, what you want to do is include your library into that crate.\NYour test file is now consumer of your li library.\NIt's where anyone else that's going to pull in your library would be.\NSo can you test it from that point.\NYou only get to test what's made publicly available.\NWhich is nice.\NIt's like better, true, black box integration test.\NBut again, it's not necessarily what I expected.\NSome of the nice things about Rust testing is the random order testing by default. What a lot of languages have that built into their frameworks now, that didn't initially run with those things beforehand.\NSo testing says all of your tests, instead of running in a linear process, they're just going to get randomized.\NYour last one will get run first and so on and so forth.\NWhich is great.\NIt's great this dedefaults where the test you run first, runs second. Dialogue: 0,9:59:59.99,9:59:59.99,Default,,0000,0000,0000,, Dialogue: 0,9:59:59.99,9:59:59.99,Default,,0000,0000,0000,,Another thing Rust has is parallel testing and parallel testing sounds great at first.\NBut let's talk about frustration.\NSo if I've got some code in Ruby and I'm going to explain this the top function does a little bit of setup.\NIt creates a directory that I'm going to do some writing to in my tests.\NThe teardown clears up that directory completely, and the tests themselves, actually just read a file, and assert to see whether weather that file exists.\NSo it's not doing too much, but there's a lot writing.\NAnd in Ruby, these tests pass everything single time.\NIf I port this over to Rust,\NI'm not using anything particular framework.\NSo the setup is specifically in my test.\NSetup does the same thing:\NMakes sure that the directory exists, the teardown makes sure the directory is clear and then the test actually just writes a file and asserts to see if a file exists.\NIf I have 10 tests like this,\NI'm going to say 8 out of 10 of them are going to fail every single time, and they're going to fail differently.\NIt's not going to be consistent which one will fail but they're going to continue failing.\NWhen I started learning Rust,\NI legitimately ran into this problem.\NThis exact problem and more hair was pulled.\NI was sitting in a hotel in Portland, like, trying to blame it on everything I could, run into code and new VM's.\NThe code works fine in Ruby it's going to work fine here.\NAnd the problem here with parallel testing is that in a way where you're doing a lot of I/O\Nor sharing resources, you're going to run into problems like this.\NThe problems that actually are occurring, your fist test has started to run.\NIt's make sure the directory exist.\NWritten its file, done his assertion, to make sure the file exists and the second test comes along.\NMake sure the directory is there.\NAnd the assertion, first test goes, okay I'm done can't here, let's just clear this correctly out.\NAnd the second test is my file is not here anymore.\NEverything's failed.\NEverything's terrible here.\NAnd if you don't know what's happening, if you've come from a language where things don't run in parallel by default then it's not going to come to your mind at first.\NOh.\Nyou know, things are running if a parallel execution.\NIt took me a long time to realize, this was actually happening.\NBut now they know it's happening.\NI know how to work around it.\NI know how to write better tests and not share resources.\NWhat this has done, all of my tests tar right, I'm going to think about where my code is going to run.\NI don't know if my code's going tend up being run parallel.\NI give it to someone, they can run it parallel.\NHave I to make sure there's not going to be a detriment to that happening and I'm going to write better tests.\NWe're going to talk about frustration last time.\NThe definition of frustrate by Webster Miriam, I believe, was to -- Dialogue: 0,9:59:59.99,9:59:59.99,Default,,0000,0000,0000,, Dialogue: 0,9:59:59.99,9:59:59.99,Default,,0000,0000,0000,,Have I one thing to accomplish and I can't.\NAnd that's frustrating.\NIn programming, frustration can frequently mean we're learning something new.\NOnce you have a better understanding of why those things were frustrating and you can see them clearly, you my actually find yourself applying in different places.\NCan you learn to appreciate those things that were frustrate being for you, initially want so now, even if you're writing more Ruby code and your Ruby code runs linearly, you're going to think about that problem you ran into, when you were writing Rust.\NI'm going to think about writing better test to make sure those things couldn't go possibly wrong.\NNext time I write a function of Python I'm going to think, what were the possible result types.\NLike, what things can go wrong?\NAm I going to get responses that are just like, I'm not expecting?\NIs the conversion going to come out properly?\NCan I verify that before I work with that data?\NA quote from Henry Ford.\NFor the world was built to develop character, \Nwe must learn that the setbacks which we endure, help us in our marching onward.\NThat said, the unfamiliar can be challenging.\NBut there can be so much to learn and love, well overcoming can.\NAnd once you have, you'll come out a better programmer than when you went in.\NI'm Brian Pearce.\NThank you very much for listening and it's been a pleasure to be with you here today.