Rust from Python and Ruby

0 0

alright can you hear me alright so um this is this was originally honest as i talk about heightened voice game programming or something and then mozilla updated their rust programming language to be more real-world useful and i figured might actually be an interesting idea to make it talk about this but i'm normally a pipe in programmer so i have a lot of like focus in the piping community for web stuff but also did a little bit of a ruby I kind of know how the language that interpreter as I have some really experience but what is rust rust is a new programming language by the guise of Mozilla but it's also a project very heavily under construction to give you an idea the language just dropped support for classes like proper classes with the last release they are changing keyword names around so they changed rat to return and a whole bunch of other things so it's really really strongly under construction so it's not something you would make a program with at the moment but it does show some really interesting things you can do with the language or what you can do with programming language is going into the future so this is quite good I guess all of you know what it does it iterates over a list of names all these names are strings and then it prints hello name nothing too spectacular the same in Ruby looks like this so the difference here is you have in each method on the list and you invoke this and pass it the block and just block them execute every time a list item comes by now can I see a show of hands how many of you are doing Python how many of you in Ruby at javascript all right so seems like roughly 5050 between Ruby and Python so how many of you that know pie tin don't know Ruby interesting so the majority actually know both languages is some that's perfect so if you keep this in mind this is how piden looks like it's for something in a list and this is Ruby which is basically do you have an array and then there is a method each on it which is invoked for all these items going from there to rust it kind of looks like a combination of both so you have the afore part and then you have an each method unless the troll and then the only thing that really changed from Ruby darest of the keyword was introduced and then the arguments move to the left side of the block and this is actually really clever idea because that means it's possible without doing any weird tricks and a lecture so every time there is this like pipe symbol comes by it knows right it's eat an operator or it's the start of it block and depending on the context it can unambiguously decide between those two and the other thing is that across is a compiled language and has types and has really really strong types but you don't see any like this has to be a string or something it works because types are inferred in rust so the only time you actually have to define the types if they pass to function so you have to tell the function you're operating on this type and Trust also has pointers and a whole bunch of other stuff for instance this is a list of four integers and then it invokes the map method similar to Ruby and there you see there are pointers involved because map map basically puts the passes the a pointer to that value instead just the value instead of copying it says right this is a pointer so you have to DVR Francis by putting a star in front of it and again no types all of this is inferred the let statement sees all right this is a list of strings because the return value of the map function was a string so it's really not that far from programming Ruby or Python it's still feels like a very dynamic language and going into the future one thing that will be possible is instead of doing this dereferencing thing you can also say actually I'm going to take a reference to accent then you can just do x times X and convert it to a string so again it this is something that doesn't necessarily work right now this was basically changed this week to support it and it's partially broken at the moment but it will eventually work so one thing that's interesting of Russ's it tracks the lifetime of things so in this case it says X is 42 and then why is a reference to X a point to X Y what's then interesting it says set it doesn't say set is why it says the reference offset is why so now you can use set as if it did letter set as if it's the visit of us X and you can reassign this and all of these are immutable so you can't actually say X is 23 will tell you I can't modify X because it's in an immutable storage compiling that it's pretty simple you just have this file your compilat dash fiat's the debug symbols and then you can run it and this compile like you don't have to use any build tools like I know make or something you can use it if you want but the language in itself is enough its support for running invoking all to compile all the files that go together so you can define what's called the great file that says this project depends off these files and this it works very much like rust index itself does and compiles you everything together into one huge binary so if this project would exist out of three files I would have a create file to test this there's an import for this import for this import for this and you just have to compile the grade file and you get a ready-made program out of it so in a nutshell what is rust it's immutable by default so every time you want to make something that is modifiable you have to put the mute keyboard in front of it it's statically typed but all types of locally in thirds you don't actually have to specify the types for the most part there is no null pointer in the language there is every pointer has to point to something and it's supported by the compiler from start to finish you will never have unless there's a bug in the language you will never have a segmentation fault because a pointer points to something doesn't true and if you're saying this is like something you don't have a problem with in Python and you will never have an ill by accident or you will never have nonetype has no attribute something like you do in Python because the compiler ensures this will never happen it has lightweight green threats as well as Colonel a bus routes see concurrency is solved on the language level it's not something that's added on top of the language like green design Python and it's ahead of time compiled but it also chip compiler if you want to use it and it also has soon interactive shell where you can explore language which is interesting for compiled language and it's fully see compatible so you can already use it with openssl or whatever c library you have and one thing that's also pretty interesting rust needs to run time like Ruby needs a runtime or like pipe needs around time but that runtime is directly embedded into your executable so you can just take this x cube or ship it to any computer and it runs so basically has the necessary parts integrated into the final executable all right let's look close at that example we had so it iterates over this thing and prints them out so what's the poor thing like why do I have to have to fork over here and that's because this code can also be rewritten as this so it passes like it takes the each takes one argument which is a function block and then this function block returns true and true means continue iterating if you would return false there it would say I'm done iterating it's like a break so this is basically just syntactic sugar for this one thing you will notice here is like there is no semicolon at the end of that line and missing semicolon at the end means implicit return I tell you everything in rust is an expression similar to everything in rust in Ruby's an expression and if you ever did some Ruby programming you have probably noticed that sometimes functions return things by accident so friends that you assign a local variable and then suddenly get return values returned from the function just because there was the last expression so if you look at the Ruby stand I read a whole bunch of functions where there's mill at the end just to make sure it doesn't return anything in rustic program is basically solved by if there is a semicolon at the end there is no return value if you leave at the semicolon the last expression is returned so this is a simple example for this there is a list of three numbers in the first there's a semi column which means the return value of the function is nil so you get the vector of three nails or if you return that number then you get a vector of three numbers mill is like nil in Ruby or non imp item and it's written as empty parentheses just for fun there is some logic behind this but I think it's slightly weird all right so there are no classes in interest but you can do abstract or oriented programming if you want and it works by a system called traits and there's a whole bunch of like Haskell logic behind all of this but unlike Haskell you don't have to be computer scientists to understand how to languish works so think of traits like interfaces in Java you have a trade that says its length printable or something and it's a method that says print the length there is no return value for this function so you just define this somewhere and then that's it that's that's the definition of what this trade is it's not that different from modules in rubidoux to say so that's here and then you need an implementation for this this would be an example implementation but this reads a little bit funky it says is an implementation that's parameterised on an arbitrary type t just basically means it works on any type and it's defined for all references which is the little m % 2 vectors of type T basically it means every list every reference to a list now gets a print length messes on it of an arbitrary type doesn't matter and then you define that function and the prince length is self that length so what you can then do is like you do blah blah blah print length so it's it's similar to Ruby so you can extend any type of the language but it's locally scoped so you have to have the trait in this straight implementation India scope or you have to have it imported otherwise print length is not on there which is very nice because it means you don't by accident have extension methods on your and the types that will clash with each other the other thing is which makes rust really unique is memory ownership tracking rust is a language without the garbage collector you can have garbage collection on individual pointers but you can write completely unguarded collected code and the way this works in the language is by tracking where memory moves so here it says let the name be Peter and there's a small tilde in front of the string which means that can only offer be one owner of that string so if i say let the new name be named and then i try to print name so first of all this will not work because it will say right there can only be one owner of this object and name has this string thing it and then you assign it to new name is this what's this so you have to be explicit here you can say I want to move this over in which case this will not compile because it says diverted the value Peter was moved out of name you could copy it then I say instead of move copy then you have this memory block copied and compile again or you just use the new new variable also one thing that's interesting unlike many other statically typed languages you can have the same variable have a different type in the next line because it will track statically what lifetime a variable has so you could totally say name is 42 a lat name be 42 and then let name B string it what's the compile so it actually tracks how memory moves and how lifetime's work so there are no null types that something really interesting so you need null in a way um but how do you solve null if there is no null in language so first of all where is the metal here any ideas or none or nil yes if there's an empty sequence to return values is none so how do you solve this problem in rust if there is no none the answer is your return what's called an option so this would be the same function it says this is a function first large it takes a sequence of integers and if another integer which is the special value and then it turns an option to an integer and this actually still reads pretty much like English you do for each item in a sequence if the item is larger than X might earn some item and if you don't return anything you return none so what you're actually doing here is you you tag your return value by either being some integer or being none if you want to use this you have to be explicitly and in this case this is how you would invoke it like get me a return value of this function and then you print that return value but you match on it so match is like pattern matching you do it's like a switch statement with a whole bunch of bells and whistles you measure the return value and then if it's some number then you say found that number if it's none then you don't have to do anything you say no number found so the solution is pattern matching and it's it's really amazing how well this works it's nicely integrated language it feels like a switch statement and since everything is an expression you can just return it and this is basically supported by a system called enums but these are not seeing a mess you can have an enum this is one single value like point but every in impart can also have parameters like a circle with a certain radius or rectangle over certain dimensions and then you can have an implementation of the enum for instance you can have a two-string method on it which matches on self if it's a point evidence point if it's circle of a specific size and the prince that size and so forth it's it's really simple to use despite the fact that there is a really expressive type system behind the language and the reason why the type system is really useful is again you will catch a lot of problems at compile time for instance if i add a new part to my enum and i don't update that method it will not compile will tell me you're missing you're missing whatever but if I would adds I don't know a quad here then I would say there is no quad in this match statement so you have to edit it will never let me compile a code that would just implicitly do something and then you can just print this and say P is a point sees a circular with four-point-oh radius and then you can print this okay so then no classes either so how do you deal with not having classes so traits are basically a solution here you can have instructed as a point has two attributes one is x1 is why both of them are floats and both of them immutable is if I don't specify mute here I will never be able to reassign the value once created and then I have an implementation for that point here there is one static method on it that says it takes an exit x and y and creates a point it turns the point x value so it's no pointer there and then there is a single expression at which busy creates an incense of that of debt structure and returns it so this is just something without message right now it has a constructor but there is no real instance method island you can either add more instant methods in here or you can also say I want to space in plement a specific trade for instance you can implement to swing trade which is a to store method which is a pure function pure means it can't modify anything on the instance and then it returns a string which is just in parentheses to throat which is x and y and then if you make a main function that uses this it has let p be the point of the origins of 0 and 0 and we can print it what the tooth third-rate basically means is that you can do some basic logic with it you can say this function takes only things I can convert into string and if the thing knows how to convert it into a string all right so how does inheritance work here's the interesting thing it doesn't really you can have trade inheritance you can have like virtual methods but all of your data is not inherited so a subclass will not see the attributes at all you have to expose these attributes through functions takes a little bit getting used to but there is a lot of lodging in it it basically means you solve things for composition rather than through inheritance there is a little bit of inheritance but use you don't use it as much as you would do in Python Ruby one other thing in rust is really quite cool how two tasks