I watched about five videos prior to finding your channel, and I was worried I would never fully grasp Struct and Traits. This video was easy to follow, you went to great lengths to explain even minor details in a way I believe anyone could understand, and you actually used numbers for height/width and showed the expected output. No other channel did that!?! Thank you so much, I am sold and just subscribed! Keep up the amazing work and the in-depth explanations with examples! You are a life saver!
Very insightful, absolutely love the videos. I've been reading a lot on rust, but there's nothing like watching your videos to truly get a sense of how the language works. Keep up the good work!
Thank you, I appreciate the comment. Its high praise to be told that my videos are helping someone learn the language. Rust might feel a bit difficult at first, but try not to go too "functional" with it and you'll be alright.
@@SilvestreVivo haha. Yeah, that happens. Just take your time and sit with the concepts for a bit. Maybe build something with them and eventually you'll get them.
@@TensorProgramming Thanks for your response. I come from the JS world and learning Rust is a hate-love feeling. I am using CodeWars to make some exercises every day to get used with it. Cheers!!!!
Rust is really awesome! I have used Go for production for 2 years and I hate its simplicity and old fashion syntax, there are so many boilerplate codes in Go. I always want to move to a better language and come up with Rust, Scala and Elixir. Rust is more difficult than Elixir but easier than Scala, it also has the best performance, so I finally settled in Rust and start learning it. I would recommend Rust to anyone who wants to build microservices, distributed system and cli tools, etc.
I'd say that Rust and Scala have about the same learning curve. Scala just is a larger language and it has more specialized features in the form of things like Higher Kinded types etc so it can feel a bit more difficult then it really is. Rust is a great language for its use-cases and it will continue to grow in the future.
The write macro is what takes care of that for us. We defined the formatter as the buffer that we wanted to write the string to which then was invoked every time we accessed the display trait for this specific struct. The formatter itself is a bit of a generic buffer in that it allows us to use other buffers since it only formats the strings for us.
Yes. Basically associated functions are functions that are in the implementation block which is associated with a type but they don't include self as the first parameter while methods include the self parameter.
A method is a function that is defined in the context of an enum, struct, or trait and its first parameter is always self. Related functions are functions that are attached to am implementation block but do not follow the method rules above.
By default, structs are stored on the stack. If you want to store something on the heap, you have to do it manually in most cases by wrapping it in a container type like a Box, RC, Vec, String etc.
Rust is more relevant then ever in terms of finding a mainstream job. That being said, many of the Rust positions that I've personally seen or been involved with generally require a higher level of experience then your typical language because the domains are more complex. Some companies are using it for web servers and APIs, and in those jobs, a junior dev can absolutely fill the position but in the case of system level development, many companies are a bit nervous to put a newer dev on their team. This should change over time however, given that the language is still seeing a wide adoption. Either way, learning a new language is always a worthwhile endeavor.
THis looks like a bit of a mess. Implicit returns? the Object constructor using parameters that have the same name as the internal attributes. That looks like begging for bugs. How am I to know which one the runtime will reference? If nothing else it completely wrecks the readability of the code.
Implicit returns are a staple of expressions. You get used to them and actually when moving to a language without them start to wonder why they aren't there. Then again, I am also a little biased because I work with a ton of functional languages. Languages like Dart have them but hide them as well. It also helps with conditional flow, since it forces you to have an else branch if you are returning something. Not to mention that you can do var assignment on a match/conditional expression. `let x = if {} else {}` etc... And you can skip calling return explicitly in functions. As for shadowed variables and constructors, its not very difficult though it can feel awkward at first. Luckily, rust has its syntactic sugar like User {name: name, email: email} can become User {name, email} which generally pushes you toward using the same names for vars and arguments. Personally, I think its more readable in many respects since you don't have to consider alternative names. As with any language that features shadowing, the var lower down the scope takes precedence. let x = 10; will be replaced by let x = 11; later with the first x being dropped from memory entirely. Its not all that different from when you use the `this` or `self` keyword in other OOP languages.
@@TensorProgramming From a programmers point of view there's a certain convenience to it, but being a professional programmer, I'm thinking more about code reviews. Where C/C++ code spells out what it is doing and can therefore be subjected to code review, Rust seems to be a lot of "reading between the lines". A code review becomes tedious and ineffective if you have to run through a whole laundry list of things that the compiler/run time may have or may not have implicitely done. How do you do an effective code review in a critical production environment, where the 4-eyes principle is mandated?
fn main () { let x : u8 = 10; let y : u8 ; y = x ; print! ("x is {} ", x); print! ("y is {} ", y); } why is it not showing error even if x lost the value
In general or for Rust? For Rust I am using RLS and Racer, there is a video that talks about this on the intro to rust playlist here: th-cam.com/video/zSIZB8WWa-o/w-d-xo.html. I use a fair few extensions for everything else however. The theme is the Spacemacs Dark Theme and the font I use is Fira Code with Code Ligatures enabled.
Ill take a look. I like the Spacemacs theme simply because its very readable on these screen casts. If the Plastic theme is more readable in a video then I definitely will use it. Thanks. Edit: It seems like a nice theme, but for some of the languages that I am using the theme shows a lot of grey on black text. This could be potentially bad for video tutorials. Ocaml and Dart seem to have this problem on some of there components. I would have to look at Rust, Go and Elixir to see if this is an issue there as well. Perhaps, I will take the black background and use it with the syntax coloring of the Spacemac dark theme.
What parts did you find difficult? Perhaps I can clarify some of them. Rust follows a paradigm that is splits the data from the behavior of an "Object". In a language like Java for instance, you would have a single class that contains both the data and the behavior but in Rust, you have a struct which defines the shape of the data and then you have an implementation block which defines behavior. Then you've got traits which are similar to interfaces in other languages. They are an interface of functions that can be implemented on multiple different types. Since you separate the data from the behavior, you can also just have a data structure without the behavior or behavior without a data structure (in the form of a trait).
Ok, that style of implementing methods looks very much like golang. Could anybody explain to me, why that is preferable to classes? In a class, I would have a constructor method and static methods which serve the same purpose as your new method. That looks like much boilerplate and chaotic code, when anybody anywhere can implement new methods.
The main reason why you would use a class in general is for polymorphism and shared behavior via inheritance. While inheritance is a tool to solve polymorphism, it isn't necessarily the most elegant one. Rust''s approach of separating data (Structs and Enums) from behavior (Impl) and using traits to group behaviors for polymorphism is a paradigm shift from your typical java/c++ style inheritance-heavy model and of course it requires some getting used to. "Composition over Inheritance" is the main philosophy behind this style of Polymorphism. Also, Enums in Rust nicely solve closed polymorphism problems in a very simple way where many OOP languages would require a ton of boilerplate. The separation of data from behavior also has its own set of advantages; for instance, passing data over foreign function interface boundaries becomes much more natural with this style of writing data and behavior. (You can just implement a trait for instance) Rust also has the explicit self arguments and methods which is somewhat orthogonal to the whole classes vs no classes argument. it is very useful to have an explicit self argument that can be passed in multiple different ways (self vs &self vs &mut self etc). This is kind of similar to the c++ const and mutable specifiers but a little bit more flexible in my opinion. Part of it will come down to opinion as with any programming language but I think if you give it a chance you will see that it is a rather elegant solution (especially when you start to look at Traits, Enums and generics). Traits can be implemented via declarative annotation macros which really cuts down the amount of potential boilerplate and helps justify this model and Enums are extremely powerful in relation to the full featured pattern matching that exists in the Rust language. Rust also used to have classes but they removed them because they were redundant and if you really wanted to re-extend the behavior of classes you could do so through the Rust Macro system. You also have to keep in mind that these are just examples and they by no means showcase the full strength of the data/behavior model in Rust. Look at some of the later project videos for a more complete showcase of this. For my own experience, I do like inheritance for some use cases but also I do like the way that Rust handles things. It really will depend on where I chose to use each of these languages and for what. I do dislike how many OOP languages obfuscate the data where as Rust certainly takes a much more data-centric approach. Coming from a background where I worked for years with languages like C++, Java and C, Rust's approach is a breath of fresh air in many ways. My advice of course, is to look at the whole tool kit before rushing to judgement (Structs, Traits, Enum and Impl blocks etc). Anyways, Cheers mate.
I really would love a video from you about your views on OOP. I am not really asking you to give a definitive answer but I feel like Data-Oriented Programming might be the better way atleast for somethings and still I am not sure. If you share your insight I think it will be extremely helpful for the community. And especially me.
Man, I've just started with Rust and I'm loving your videos. Thank God Reddit redirected me here.
I was wondering where I was getting the influx of Rust views from. Well, glad you like the content and glad to see more new faces.
This whole series is very begginer friendly and well explained and at just the right pace. Very good work.
Thank you. Glad you found the content useful.
I watched about five videos prior to finding your channel, and I was worried I would never fully grasp Struct and Traits. This video was easy to follow, you went to great lengths to explain even minor details in a way I believe anyone could understand, and you actually used numbers for height/width and showed the expected output. No other channel did that!?! Thank you so much, I am sold and just subscribed! Keep up the amazing work and the in-depth explanations with examples! You are a life saver!
Very insightful, absolutely love the videos. I've been reading a lot on rust, but there's nothing like watching your videos to truly get a sense of how the language works. Keep up the good work!
Thank you, I appreciate the comment. Its high praise to be told that my videos are helping someone learn the language. Rust might feel a bit difficult at first, but try not to go too "functional" with it and you'll be alright.
Just as I was slowing down on learning Rust, your videos just brought me back on track. Great job!
Thats good, cheers mate.
One of the nicest videos on Rust!! great job, thank you so much
Wow! Rust is literally the most idiomatic and intuitive systems-level language I've seen!
Once you get past the ownership system, yes it does become the most intuitive systems level language next to Golang.
@@TensorProgramming I am still trying to get past over there..... even so, I think Rust is beautiful
@@SilvestreVivo haha. Yeah, that happens. Just take your time and sit with the concepts for a bit. Maybe build something with them and eventually you'll get them.
@@TensorProgramming Thanks for your response. I come from the JS world and learning Rust is a hate-love feeling. I am using CodeWars to make some exercises every day to get used with it. Cheers!!!!
@@SilvestreVivo Good old JS. Good luck man.
Rust is really awesome! I have used Go for production for 2 years and I hate its simplicity and old fashion syntax, there are so many boilerplate codes in Go. I always want to move to a better language and come up with Rust, Scala and Elixir. Rust is more difficult than Elixir but easier than Scala, it also has the best performance, so I finally settled in Rust and start learning it. I would recommend Rust to anyone who wants to build microservices, distributed system and cli tools, etc.
I'd say that Rust and Scala have about the same learning curve. Scala just is a larger language and it has more specialized features in the form of things like Higher Kinded types etc so it can feel a bit more difficult then it really is.
Rust is a great language for its use-cases and it will continue to grow in the future.
Best tutorial videos for Rust introduction. Thanks a lot. Much love, man.
thanks. Glad you found the content useful.
Object Oriented Programming in Rust
Amazing video
yeah the OOP in Rust is fairly smooth and easy to learn. No messy items like Inheritance.
How did the Formatter from Display trait get resolved automagically in the last example? (Coming from Scala, didn't see any implicits either here)
The write macro is what takes care of that for us. We defined the formatter as the buffer that we wanted to write the string to which then was invoked every time we accessed the display trait for this specific struct. The formatter itself is a bit of a generic buffer in that it allows us to use other buffers since it only formats the strings for us.
@@TensorProgramming I see, thanks
so obj::new() is basically constructor for Object class ?
Well, I wouldn't call the Object struct a class since there aren't really such things in Rust but yes its basically an initialization function.
What vscode theme are you using? thanks
Spacemacs dark with fira code font.
How does it know the difference between methods and related functions. Is it just the self parameter?
Yes. Basically associated functions are functions that are in the implementation block which is associated with a type but they don't include self as the first parameter while methods include the self parameter.
Good Source for noobs thank you :)
What is the name of its Grammer extension that you are using?
I don't have a grammar extension.
so beautiful . 😭😭😭😭
very good!
Hi tensor, wat's diff bw method and relative func ?
A method is a function that is defined in the context of an enum, struct, or trait and its first parameter is always self.
Related functions are functions that are attached to am implementation block but do not follow the method rules above.
Tensor Programming Should we assume that a “related” function is more like a class/static method in some other languages?
@@TensorProgramming Thank you for the reply. That clarified stuff for me :)
Are those objects located in stack or heap? if on stack how can get them on heap?
By default, structs are stored on the stack. If you want to store something on the heap, you have to do it manually in most cases by wrapping it in a container type like a Box, RC, Vec, String etc.
i wonder why you always say have good night at the end of your videos maybe its noon here by the way i like the method you use for teaching
Its night somewhere.
Should someone in 2023 consider learning rust for stable carrier prospects or something else you would recommend
Rust is more relevant then ever in terms of finding a mainstream job. That being said, many of the Rust positions that I've personally seen or been involved with generally require a higher level of experience then your typical language because the domains are more complex. Some companies are using it for web servers and APIs, and in those jobs, a junior dev can absolutely fill the position but in the case of system level development, many companies are a bit nervous to put a newer dev on their team. This should change over time however, given that the language is still seeing a wide adoption. Either way, learning a new language is always a worthwhile endeavor.
THis looks like a bit of a mess. Implicit returns? the Object constructor using parameters that have the same name as the internal attributes. That looks like begging for bugs. How am I to know which one the runtime will reference? If nothing else it completely wrecks the readability of the code.
Implicit returns are a staple of expressions. You get used to them and actually when moving to a language without them start to wonder why they aren't there. Then again, I am also a little biased because I work with a ton of functional languages. Languages like Dart have them but hide them as well. It also helps with conditional flow, since it forces you to have an else branch if you are returning something. Not to mention that you can do var assignment on a match/conditional expression. `let x = if {} else {}` etc... And you can skip calling return explicitly in functions.
As for shadowed variables and constructors, its not very difficult though it can feel awkward at first. Luckily, rust has its syntactic sugar like User {name: name, email: email} can become User {name, email} which generally pushes you toward using the same names for vars and arguments. Personally, I think its more readable in many respects since you don't have to consider alternative names.
As with any language that features shadowing, the var lower down the scope takes precedence. let x = 10; will be replaced by let x = 11; later with the first x being dropped from memory entirely. Its not all that different from when you use the `this` or `self` keyword in other OOP languages.
@@TensorProgramming From a programmers point of view there's a certain convenience to it, but being a professional programmer, I'm thinking more about code reviews.
Where C/C++ code spells out what it is doing and can therefore be subjected to code review, Rust seems to be a lot of "reading between the lines". A code review becomes tedious and ineffective if you have to run through a whole laundry list of things that the compiler/run time may have or may not have implicitely done.
How do you do an effective code review in a critical production environment, where the 4-eyes principle is mandated?
fn main ()
{
let x : u8 = 10;
let y : u8 ;
y = x ;
print! ("x is {}
", x);
print! ("y is {}
", y);
}
why is it not showing error even if x lost the value
Its not "losing the value" since its a primitive. The 10 from x is being copied to y in this case so both have the value 10 in them.
What are the extensions you use?
In general or for Rust? For Rust I am using RLS and Racer, there is a video that talks about this on the intro to rust playlist here: th-cam.com/video/zSIZB8WWa-o/w-d-xo.html. I use a fair few extensions for everything else however. The theme is the Spacemacs Dark Theme and the font I use is Fira Code with Code Ligatures enabled.
Thanks, Love the videos
I use same extensions and same fonts but I suggest check out Plastic theme for Vscode.Much better color support imo.
Ill take a look. I like the Spacemacs theme simply because its very readable on these screen casts. If the Plastic theme is more readable in a video then I definitely will use it. Thanks.
Edit: It seems like a nice theme, but for some of the languages that I am using the theme shows a lot of grey on black text. This could be potentially bad for video tutorials. Ocaml and Dart seem to have this problem on some of there components. I would have to look at Rust, Go and Elixir to see if this is an issue there as well. Perhaps, I will take the black background and use it with the syntax coloring of the Spacemac dark theme.
Yeah it's personal opinion but I like it.
WoW this was difficult
What parts did you find difficult? Perhaps I can clarify some of them.
Rust follows a paradigm that is splits the data from the behavior of an "Object". In a language like Java for instance, you would have a single class that contains both the data and the behavior but in Rust, you have a struct which defines the shape of the data and then you have an implementation block which defines behavior. Then you've got traits which are similar to interfaces in other languages. They are an interface of functions that can be implemented on multiple different types. Since you separate the data from the behavior, you can also just have a data structure without the behavior or behavior without a data structure (in the form of a trait).
@@TensorProgramming thanks bro ❤️❤️
Ok, that style of implementing methods looks very much like golang. Could anybody explain to me, why that is preferable to classes? In a class, I would have a constructor method and static methods which serve the same purpose as your new method.
That looks like much boilerplate and chaotic code, when anybody anywhere can implement new methods.
The main reason why you would use a class in general is for polymorphism and shared behavior via inheritance. While inheritance is a tool to solve polymorphism, it isn't necessarily the most elegant one. Rust''s approach of separating data (Structs and Enums) from behavior (Impl) and using traits to group behaviors for polymorphism is a paradigm shift from your typical java/c++ style inheritance-heavy model and of course it requires some getting used to.
"Composition over Inheritance" is the main philosophy behind this style of Polymorphism. Also, Enums in Rust nicely solve closed polymorphism problems in a very simple way where many OOP languages would require a ton of boilerplate. The separation of data from behavior also has its own set of advantages; for instance, passing data over foreign function interface boundaries becomes much more natural with this style of writing data and behavior. (You can just implement a trait for instance)
Rust also has the explicit self arguments and methods which is somewhat orthogonal to the whole classes vs no classes argument. it is very useful to have an explicit self argument that can be passed in multiple different ways (self vs &self vs &mut self etc). This is kind of similar to the c++ const and mutable specifiers but a little bit more flexible in my opinion.
Part of it will come down to opinion as with any programming language but I think if you give it a chance you will see that it is a rather elegant solution (especially when you start to look at Traits, Enums and generics). Traits can be implemented via declarative annotation macros which really cuts down the amount of potential boilerplate and helps justify this model and Enums are extremely powerful in relation to the full featured pattern matching that exists in the Rust language.
Rust also used to have classes but they removed them because they were redundant and if you really wanted to re-extend the behavior of classes you could do so through the Rust Macro system. You also have to keep in mind that these are just examples and they by no means showcase the full strength of the data/behavior model in Rust. Look at some of the later project videos for a more complete showcase of this.
For my own experience, I do like inheritance for some use cases but also I do like the way that Rust handles things. It really will depend on where I chose to use each of these languages and for what. I do dislike how many OOP languages obfuscate the data where as Rust certainly takes a much more data-centric approach. Coming from a background where I worked for years with languages like C++, Java and C, Rust's approach is a breath of fresh air in many ways. My advice of course, is to look at the whole tool kit before rushing to judgement (Structs, Traits, Enum and Impl blocks etc).
Anyways, Cheers mate.
I really would love a video from you about your views on OOP. I am not really asking you to give a definitive answer but I feel like Data-Oriented Programming might be the better way atleast for somethings and still I am not sure.
If you share your insight I think it will be extremely helpful for the community. And especially me.
It's like reading the Bible - every time I watch, I never fail to understand more.