Focus mode

Rust Fundamentals

Generics and Lifetimes

Generics

Generics, traits and lifetimes are other important subjects that should be learned, since these concepts makes Rust better to its peers. Polymorphism, one of the foundations of object-oriented programming, derives from traits and generics in Rust.

Generics is the structure that prevents duplication in Rust. A syntax that allows the developer to use generic terms as placeholders for more concrete types to be passed in elsewhere and does not require defining different functions of different types. When creating code, we can define generic attributes such as behavior or how they relate to other generics without knowing what will actually be in their place. Generics make the code more flexible. 

To understand generics in more detail, additional resource can be found here:

https://www.youtube.com/watch?v=6rcTSxPJ6Bw&t=11s 

You can find detailed further information from Rust Official documentation:

https://doc.rust-lang.org/book/ch10-00-generics.html 

Resources:

https://www.geeksforgeeks.org/rust-generic-function/ 

https://alexeden.github.io/learning-rust/programming_rust/11_traits_and_generics.html 

Lifetimes

Lifetimes are another kind of generic that ensure references are valid for as long as we need them. To minimize memory usage by reducing unnecessary storage lifetimes are good structs to use. 

In Rust, every reference has a lifetime which is the duration for which the reference is valid. Where more than one species is possible, or where references can be associated in several different ways, we should annotate species and lifetimes to avoid problems. 

You can find detailed further information from Rust Official documentation:

https://doc.rust-lang.org/book/ch10-03-lifetime-syntax.html 

Resources:

https://www.buraksenyurt.com/post/rust-pratikleri-lifetimes-mevzusu(in Turkish)

left-disk

Programs to Accelerate Your Progress in a Software Career

Join our 4-8 month intensive Patika+ bootcamps, start with the fundamentals and gain comprehensive knowledge to kickstart your software career!

right-cube

Test

Comments

You need to enroll in the course to be able to comment!