Nah now i’m live i guess what the heck is going on here like seriously um so did i did i said correct stuff here now yes is this is is this actually oh my god he died so welcome to this advertisement episode for lgl.silicon.com let me retype it lgl stands for laserguckenland dotsillyhoon.com Not comacom.com it’s a freely reachable high available plan to stay on for eternity minecraft vanilla so where you can play free and i still don’t know how to make these machines this is my fourth try of uh making this advertisement episode again Um i didn’t do it in a while in a while and it seems like i totally forgot how like the internet works um yeah but i felt motivated to advertise the server again as you can see there’s only two players online so it desperately needs more advertisement that people actually play here Oh where am i am in the corner that i wanted to be yes this is t1 yes we still have a machine here okay cool but before i actually start playing i should make sure that we have audio and video at the same time um yeah it it doesn’t seem like It it doesn’t seem like where am i even streaming to stream settings where is it like where can i where is where is this what am i doing here okay how how can one suck so hard oh i’m live wait it’s how how can one suck so hard It’s a different url though wait No way ah stop this echo what the heck mate why does it still show the old stuff okay so well let’s remove that shitty okay whatever uh yeah let’s let’s just it’s still the old let’s just start i guess um what a mess okay let’s i will no Playback the video if you’re interested in the server watch any other um stream okay so uh my talk is about uh rust for superspas developers and the idea here is to give simplest developers a jump start to the ras programming language comparing contrasting the languages so that it’s easier to Kind of get into if you are a single class developer or has have some background in class class so my name is pavel i’m a developer trainer author i have some books that i’ve written they’re all available online and like i always say please buy them and make me money So we do that i offered a few uh tools that i’ve seen that intro already yeah it doesn’t then it won’t so we can’t really know up front what you expect in some of these temperatures so this is somewhat is going to be mitigated in synthesize 20 let me check the audio again Already takes one step ahead to mitigate that and make this always safe so that’s some kind of introduction the most i think difficult parts in uh grabbing with rust is the idea of the ownership model this is the fundamental change i think this is the kind of the challenge from when moving From c plus plus or trying to understand rust based on what you know in c plus so in c plus plus you can have a single owner or a shared ownership there are various types such as the shirt pointer you can have a single owner with the unique pointer but really Um you’re not enforced in any way you can make mistakes for example you can pass a raw pointer to some function that function may decide that it is going to keep that pointer for later which is of course a bad idea because if you intended to share that pointer You should have passed along a shared pointer but the compiler cannot enforce that you are left on your own in rust you have a single owner a model at the basis and that’s explicit and we see that this is something that allows the rust compiler to make sure we don’t make mistakes no You can’t make sort of maybe silly mistakes but they’re not so silly when you have a very large system on your hands and so from point of view the developer has to know what it’s doing that’s always been the case i’m not saying it’s good or bad i’m just Saying this is the way it is this is the expectation the expectation from the sip asses developer is to do the right thing in russ the compiler is the one responsible for making sure we don’t mess things up and so we have something called the borrowed cheater in rust Which is responsible to make sure that we don’t do things in the wrong way i’ll show you a few examples in a moment and so in c plus class we have assignments and copy construction and by default when we assign one value to another they’re just being copied the copy constructor or Our assignment operator has been called unless we have an r value on our hands or use the the move function and stuff like that in rust this is kind of the opposite so the the model is everything is first moved so if you assign something you move the ownership from one variable To another variable and so the previous variable now has nothing if you try to use it it will not work and i mean the compiler will tell you that you can’t do that because it’s no longer the owner of the object and so what you typically do you borrow The reference to the object to do some stuff with it and then kind of return it to the owner so typically we’re going to borrow references wait like the ball checker make sure you’re not making mistake for example you cannot borrow something and outlive the something that you referencing That can never happen the compiler will make sure of that in simplest class it’s your responsibility if you take a reference to something so you will not use it after that something may already be dead so enough talk let’s see some code so here’s a very very simple example Here’s a c example we have a vector v one that’s initialized with three values nothing very spectacular there and then i’m assigning that or copy constructing that to v v2 and v3 and because v1 is an l value in terms of in c plus plus terms then the copy constructor is going To be activated and once we run this what we get is really three distinct vectors they have no connection to one another each one is independent we have a deep copy going on here with these vectors each one can be developed further in its own columns unconnected to the other vectors if you Try the same thing in russ this is what it looks like so in russ we declare variables using the let statement by the way you’ll notice that in rust all the keywords are as short as possible so function is fn and let is very short so rust is really about keeping things short And what we see here is that we are initializing v1 using a macro in rust when you see the exclamation point it means a macro it’s not the same as a c macro don’t worry about it’s not some kind of copy paste that the preprocessor does and then You have to deal with the aftermath it is something different but for our purposes it creates with three numbers notice i didn’t have to specify the type rust always or at least does a lot of work to infer types automatically but it is statically typed so it’s nothing like some Variant some javascript object that can grow this is exactly a vec of a i32 which is the 32-bit something is wrong here so now when we do something like v2 equals v1 we actually transfer ownership from v1 to v2 so now v1 holds nothing and then the third instruction Trying to move v1 to v3 will actually fail but it will fail in compile time so we get this nice error from our rust compiler so notice i mean if you’ve ever done class development and using templates and you have a compilation error good luck with that so you’re looking at The arrow and it’s kind of maybe 200 characters long and those braces the triangular braces are all over the place you’re not not sure what you have to do here last compiler is really different i think that statement from the marketing slogan at the beginning is completely correct So it says hey value moved here line 17 value used here after move so you’re getting a very clear error message and in many cases the rust compiler will also provide suggestions of how to fix them and so really i think i’m very happy with the rust compiler because it really helps me To do the right thing and so how can we fix that so one way to do that is use a specific method on vector which is called clone that’s because vec implements a trait or you can think of that as an interface or an attribute which is called clone which has a clone Method and it really just makes a copy of itself and so now v2 is a completely independent copy of the vector same goes for v3 and then you get to the same uh place as the c plus plus code this just to give you an idea of what’s the Expectation in russ coach so assignment is really moving it’s not popping unless you call a specific method if of course that method exists that’s a very simple example to start with so here’s another simple example that shows you borrowing in action so let’s say i’m writing this function So this is a function in rust you can see that functions are declared with the fn keyword give a function a name and then the parameters are given in this way there’s was it even a detector rate i don’t know the variable name column and the type in this case i’m using The standard type string and this is returning nothing so i can just go ahead and implement that function notice by the way that the curly brace is on the same line this is a rust grid style now most people like the curly braces on the next line So i hate it so i love rust because of that at least all the rest is just nonsense but the curly braces are are the most important thing and so now i’m in my main function and i want to call this function he pronounced that joke way too serious Associated that is static method called from and the reason i’m doing that i’m not just providing the the string which will not get into so i’m going to create a real dynamic string which i can even change later if i wanted to and then i’m calling greet Netflix wait can you even hear my desktop audio like if i if i mute my what the heck why does it i messed up my audio settings didn’t i audio desktop audio default built a in definition audio controller what i don’t get it whatever my life and because i’m passing a name To the method i’m actually passing ownership and so this code does not compile why because on the next line i’m doing something with name but name is gone so it’s now an empty sort of pointing to nothing there’s nothing there so the compiler how long is it lying allow me to Have this code so it says look value moved here so you’re trying to use it again here you can’t do that that’s against the rules and so the solution to that is fairly simple and you just need to get used to that so here’s what i need to do The function grid is not going to get the object string but a reference to it because i just want to do something with that string and that’s it i’m not trying to store it anywhere i’m just going to use that as an input parameter So in this case i the call to the grid function passes along a reference to the string so i have to put that ampersand in the declaration of the function and in the call side where i’m calling the function then you can go ahead and use name just fine so this is a Simple example of borrowing a reference and using that temporarily the compiler will make sure that the lifetime of the reference is not outlived the lifetime of the object it is referencing in this case it’s very simple to see but there are some other cases which are much more complex Let’s see another example where we actually thank the compiler for not compiling our code so here’s some c plus plus code which looks fairly innocent i’m creating a vector of string and then pushing a string in there and then i look at that value in v0 and then just displaying that then i’m Pushing another string and displaying the previous value again so this looks innocent enough but if you try to compile it doesn’t care about anything if you try to run it i try to run it in visual studio in this case this is what i got in A debug build it says something bad has happened there’s an access violation it’s an arrow to get to a location which has the real objects it’s not going to work clearly that’s not a good idea so if you look at the code if your syntax does develop and Think a little bit something here is problematic and the problem is maybe not so easy to spot even though we have only about four or five lines of code and the problem is that the second push needed to increase the capacity of the vectors it was relocated To another region in memory and the existing strings were just copied there and so the previous reference was now referencing an old data which wasn’t longer there it’s just not there and the compiler cannot really help me the terminal this is problematic i’m going to hit problems at runtime maybe not all the Time if i if by chance there was still room in that vector the capacity was higher and the relocation was not needed then that would work but then i deployed the application and in some cases after adding more more strings and using this kind of code it would crash So i’m going to debug these things and this is no fun it’s better if the compiler can tell me up front this is a bad idea so if i try to do that in rust here’s the sort of the equivalent code i’m creating an empty vector Notice the mute keyword so in rust all variables are immutable by default so we cannot mutate and you cannot change them if you do want to change something you have to add the mute keyword again everything is shortened as much as possible which again gives the intent you’re opting in Saying hey i declare my intent to make changes to this object which i think is better than the opposite most languages take the opposite stats you can do anything with the variable and then in c plus pass you have to seek the cons in various locations To say hey i’m promised i’m not going to do any changes to that particular object and so i have an empty vector here and pushing hello and then i’m taking a reference to v0 in exactly the same way as the c plus plus code and printing that value And then pushing another value and printing the previous value as well so we try to compile that and get another compilation here that says hey you borrowed a reference here immutably and then you change something here so you mutated something while you have an immutable reference to the object That’s exactly a data race and because of that data race the compiler says hey i’m not going to allow them so the compiler is of course very conservative because it may be the case that this vector also has more capacity in it and maybe that actually will not crash in random But the rust compiler is not taking any chances so it forces the developer to do things differently to make sure that a hundred percent that nothing will bad will happen so of course that means there is always in programming there’s no free lunch you can’t really get everything So you do need to do some things differently even if you can guarantee that that vector will not need any reallocation in this simple scenario and so i haven’t provided a solution here yet we’ll discuss so it will not work in exactly the same way because once you have a mutable reference You cannot have any other references to the same thing mutable or immutable that’s the basic idea if you have only immutable references you can have as many as you want because they can just read the data but once you have one with reference that’s it you cannot have any other references in The same score that’s the idea here okay so here’s again some brief comparison from terms known in c plus plus to stuff that we that are implemented in rust and so this model of borrowing may seem like perhaps a good idea but it looks kind of limited because how can i Share an object between multiple elements maybe multiple threads even it’s not good enough to have just a single owner so we need something more and so us provides this as part of its standard library so for instance sleepless class we have unique pointer unique pointer is a very simple Something that allows us to have a single owner to something that is allocated typically on it and so rust has an equivalent called box so i can take anything and give it to box uh column column new and that would be uh stored on the e and so the box will kind of Point to that something but boxes is a bit easier to use than unique pointer because of a trait that rust supports you can actually access the t object uh implicitly without doing any special dot get or something like this to get to the underlying pointer so it’s a bit easier to use Because this is a very common thing to do for shared pointer the t plus class provides the equivalent in rust our rc this is the the single threaded reference counter and we have arc which is the atomic reference counter so if you need to share an object Between threads you have to use arc and that implements certain traits that allow it safely to move to from thread to thread so these are kind of the the terms that you can uh see here which make things a little easier like what the man so references exists in c plus plus of Course and as we’ve seen in rust references implies borrowing and the borrower checker will make sure stuff is done correctly and so the defaulting c plus plus in is non-constant you want something to be constant you have to explicitly specify that and rush this is exactly the opposite everything is Immutable unless you specify a mute with a new keyword that you want something to change so let’s see something a little bit more complex perhaps and so let’s say i want to do something like this i want to calculate the number of prime numbers between some first and last number and i Want to do that with multiple threads so i’m going to do a very simple chunking by giving each thread a range of numbers to work on and they’re going to work on that concurrently and then give me back uh each one will give me its result and i’ll just kind of Add the result up so that’s a very simple relatively simple thing to do in a multi-threaded world and so how would you do that in c plus plus so here’s uh is an example of how to do that i have a simple is prime function that just knows to calculate Whether a number is prime or not that’s uh fairly straightforward and here’s the main function called calc primes and so card price we see the number of threads and the range of numbers for which i would like things to work and so the idea here is that I’m going to create several threads each thread is going to get a chunk a front and a two to work on which i calculate here and then it’s going to run in a loop and check each one whether it’s a prime number or not and if it is a prime number I’m going to push that into a vector i maintain here now if you’ve done any and then of course i have to wait for the threads to finish using this during that and if statement it was simply not compiled i can’t do that i can’t just transfer Ownership to a different thread at first because there’s no guarantee that thread is going to be alive while that vector is alive maybe the main thread will terminate before that and that and then we’re going to have a reference to something that is already gone so the compiler will not let me make These ceiling mistakes and so the fixed version looks something like this my data is a new vector but here i have a mutex that takes that data so the mutex is the owner of my data in a sense so the data is hit is hidden behind the mutex So if i want to get to the data i have to actually tell the mutex lock lock means acquire the mutex please give me reference to the actual data while the mutex is locked and when that variable goes out of scope after i do a push operation then the mutex is Released so this is the general idea and and at the end of this loop i want to get to the actual value again i have to do lock here to get to the actual vector back and then return the size of the vector because the purpose of my function is just to Return the count of primes and not the entire list in this case and so again notice i don’t have any return statement just writing something some expression without the semicolon and so when i compile it and it compiles fine i’m guaranteed i am confident this code is bulletproof so it’s going To run correctly anywhere because again the compiler is very conservative even if there is a tiny chance this would fail in some mysterious situation it will not let me compile this code okay so let’s move on another thing i want to show you which is interesting in rust Are enumerations so enumerations the z plus plus are are fairly simple just a set of values you can specify the exact value you can specify the size of that enumeration whether it’s four bytes or two bytes or eight bytes and so on but really there’s not much to it this is just A set of values now in rust enums are are different so they can’t be just like the c plus plus ignoms that’s not the problem here’s the season in nam that we’ve seen earlier in a rash style which is practically the same but enough can be more interesting here’s the ni nam which Is a generic noun that works on two parameters called t and e here that has two values which in themselves are really a family of values so one of them is called okay with some object and the other error with some other object and so this is in Fact one of the types in the ras standard library which is used for reporting errors from functions so the typical function that may fail should return result of something and something so the first something is what happens if the method works correctly what the type that is going to be Returned and the second something is a type of error we should provide the error information in case something doesn’t work correctly the return the actual return statement in that function are going to be okay something or err something and so the calling code can look at that result And there are in fact methods in enum so nums can have methods so it’s easier to work with the the result so if i maybe i want to look at the error maybe i want to look at the result maybe i don’t care about the air because i’m Pretty sure that’s going to be fine so i can use the something like unwrap and just take the result and so on so it’s very rich thing to have another one which is very common is called option again it’s very common to have that as return type from functions which is very Similar to the c 17 optional type which means either you have something or you have nothing so if you have something t will be the something and the value is some something or you have nothing so we get back the non value from the num so here’s A different example those of you who are old enough maybe are familiar with a language called logo and one programming logo at some point in their lives i did that in some point and so in logo there is that thing called the turtle uh until today i Have no idea why it was called the turtle because it was moving sometimes very fast and it’s very unlike a turtles i’ve seen but it is a turtle and nonetheless and so if i want to model that for instance here’s one way i can do that I can create an enough to to specify what kind of commands this turtle can uh can work with what the things that he can do so in c plus plus i would have to just have a set of values and then have some way maybe some substructures that provide The actual data to some of the commands maybe using a union so here we get that for free so i have a turtle command some of the commands here don’t have any parameters such as rotate right and rotate left some of them have parameters such as forward and backwards And even pen color can have another object that has three fields r g and b which which are representing the rgb values of some color for instance and so here’s how how i can work with something like this let’s see uh turtle well anyways i guess that’s it for this stream i I totally lost motivation but please play here so much Video Information
This video, titled ‘Minecraft Anarchy- Rust for C++ developers – What you need to know to get rolling with crates’, was uploaded by ZillyGurke on 2020-11-14 00:59:36. It has garnered 14 views and 0 likes. The duration of the video is 00:35:57 or 2157 seconds.
Lasergurkenland anarchy server domain: lgl.zillyhuhn.com
Small pure vanilla minecraft server. No plugins. No admins. No rules. Chilled anarchy server with stable tps and no queue. No world resets and stable uptime. The server will stay online for at least a few years.
NDC Conferences talks watched in this video: Rust for C++ developers – What you need to know to get rolling with crates – Pavel Yosifovich