0/5
# Generic type ## Example Execute the following command to run [`./solutions/examples/generic.rs`](https://github.com/Cyfrin/rust-crash-course/blob/main/topics/generic_type/solutions/examples/generic.rs) ```shell cargo run --example generic ``` ## Exercises Exercises are in [`./exercises/src/lib.rs`](https://github.com/Cyfrin/rust-crash-course/blob/main/topics/generic_type/exercises/src/lib.rs) ### Exercise 1 ```rust pub fn first(t: (u32, i32)) -> u32 { t.0 } ``` Turn this function into a generic function. ### Exercise 2 ```rust pub fn last(t: (u32, i32)) -> i32 { t.1 } ``` Turn this function into a generic function. ### Exercise 3 ```rust #[derive(Debug)] pub struct Rectangle { pub top: u32, pub left: u32, pub width: u32, pub height: u32, } ``` Turn this struct into a generic struct. ## Test ```shell cargo test ```
Execute the following command to run ./solutions/examples/generic.rs
Exercises are in ./exercises/src/lib.rs
Turn this function into a generic function.
Turn this function into a generic function.
Turn this struct into a generic struct.
An empowering dive into Generic Type in Rust - Learn how Rust's generic types enable you to write flexible and reusable code by abstracting over types in functions and data structures. You'll put this understanding into practice by refactoring tuple-accessing functions and a `Rectangle` struct to make them generic.
Previous lesson
Previous
Next lesson
Next
Give us feedback
Course Overview
About the course
Introduction to the Rust programming language
Rust variables and functions
Scalar types, arrays, strings, enum, structs, vectors, and hash maps in Rust
Rust control flows: If / else, if let and let else, loop, match
Rust ownership, including borrow and references
Rust error handling
Rust Modules
Rust Traits
Last updated on July 11, 2025
Rust Developer
Rust Programming BasicsDuration: 6min
Duration: 18min
Duration: 47min
Duration: 15min
Duration: 19min
Duration: 8min
Duration: 12min
Duration: 46min
Duration: 14min
Course Overview
About the course
Introduction to the Rust programming language
Rust variables and functions
Scalar types, arrays, strings, enum, structs, vectors, and hash maps in Rust
Rust control flows: If / else, if let and let else, loop, match
Rust ownership, including borrow and references
Rust error handling
Rust Modules
Rust Traits
Last updated on July 11, 2025