Rust is the ideal language for writing safe, correct code in a way that won't make you pull your hair out. This course will teach you how to build reusable Rust code so that you can stop copying and pasting code. Write code that can adapt to many different usages.
You will reuse code by using advanced features such as traits, generics and macros. You will work with different forms of code reuse, loops, map, filter and fold to save time and resources. Achieve higher-level reuse without sacrificing runtime performance. Organize your code into modules and crates to publish them to crates. io.
Rust is the ideal language for writing safe, correct code in a way that won't make you pull your hair out. This course will teach you how to build reusable Rust code so that you can stop copying and pasting code. Write code that can adapt to many different usages.
You will reuse code by using advanced features such as traits, generics and macros. You will work with different forms of code reuse, loops, map, filter and fold to save time and resources. Achieve higher-level reuse without sacrificing runtime performance. Organize your code into modules and crates to publish them to crates. io.
By the end of the course you will be able to avoid code duplication and write clean reusable code.
About the Author
Shing Lyu is a software engineer and open-source promoter. Shing contributed to Mozilla's Servo and Gecko (Firefox) browser engines using Rust, and is active in Rust study groups, training, and conference talks. Shing is passionate about coding, automated testing, and promoting free and open-source software craftsmanship.
This video provides an overview of the entire course.
How can we try the code examples from the course? By setting up the Rust development environment.
• Understand the options available: playground and rustup
• Learn to test simple code using the online playground
• Learn to install the toolchain locally with rustup
A brief overview of the code reuse mechanisms in Rust, which we'll discuss further in the course.
• Understand the considerations of code reusability
• Learn what Rust provides us for code reuse
How do we avoid repetitive code using loops and properly use iterators?
• See why for loops only accept iterators
• Learn what the IntoIterator trait has to do with the for loop
• Understand the ways to get different iterators with or without reference
The aim of this video is to learn an alternative way of doing loops.
• Learn iterator adaptor and consuming adaptor
• Know when we should use functional programming loops as against for loops
• See how lazy evaluation can help you do more than a for loop
How we can use one of the most common features in programming languages—functions—to avoid duplication?
• Understand the basic syntax of functions
• See how functions take typed parameters
• Learn the common mistake of moving ownership into a function
How and where can generics help us to reuse code in Rust?
• Learn how generics can help us reuse high-level algorithms
• Learn how generics can help us reuse high-level data structures
• Understand where generics can be applied
How can we reuse an algorithm for different types? Generic functions can help us abstract away the types in a function signature.
• Understand the basic syntax of generics in functions
• Know why not all types can fit into all generic functions
• See how we can use a trait bound to restrict the types used in a generic function
How to reuse common data structures for different types? With generic on structs and enums.
• Learn how generic struct can abstract away the types
• Learn how multiple generic types can be used in one struct
• Understand how the same concept can be applied to enums
The aim of this video is to learn how struct methods can use generic just like normal functions.
• Learn the basic syntax of generic on struct methods
• Understand why we need impl<T> instead of just impl
• See how we can implement a generic struct method for only certain types
Show how generics are used in the Rust standard library.
• See how option<T> uses generic to represent optional values
• See how result<T> uses generic to represent a success/fail result
• Know how to use option and result in error handling
The aim of this video is to showcase how generics are used in the Rust standard library.
• See how collections like vector or hash use generics
• Learn how smart pointers and wrappers use generics
• See how we can compose wrappers using the generic syntax
The aim of this video is to learn the basic syntax of trait and trait bound.
• Learn the basic syntax of trait
• See how trait can help us define interfaces
• Learn how trait bound can help us enforce interfaces
How can we enforce and use a trait without knowing the solid type? Trait bounds and trait objects can help us here.
• See how trait bounds are used in a real-world example
• Learn the "where" form of trait bound syntax
• See how a trait object can be used instead of a solid type
Using generics on traits may cause us to accidentally create multiple implementations. Associated type can guarantee that it's only implemented once.
• Understand why generics on traits may cause multiple implementations
• See how associated type solves this problem
• Learn how trait inheritance can enforce more complex trait relationships
Traits and generics have different effects on performance. Learn the difference between static dispatch and dynamic dispatch.
• Learn about static dispatch and the code bloat problem
• Learn about dynamic dispatch and its performance impact
• See how a trait object is implemented internally
How do we do operator overloading and type conversion in Rust? The Rust standard library uses traits for this.
• See how operator overloading can be achieved by traits
• Learn how lifetime and function calls also utilize traits
• Understand how From and Into traits help us do type conversion
How to tell the compiler that our custom type has a special property? Marker traits will help. We also talk about some useful std traits.
• Learn about marker traits and how they interact with the compiler
• Know how to define custom formatting with Display and Debug
• Learn how a default trait can be useful in defining defaults
Learn what metaprogramming is and the tools available in Rust.
• Learn what metaprogramming is
• Understand the options for metaprogramming in Rust
• Know when to use metaprogramming
Some code repetition is not possible to eliminate by the language itself. We can use declarative macros to abstract away that.
• Understand the syntax for defining declarative macros
• See a real-world example of vec!()
• Learn about the namespace for macro and why we need macro_use
Implementing repetitive traits is cumbersome. Using procedural macros, we can create custom derive rules to reduce clutter.
• Learn how custom derive works
• Learn how we organize the folder structure for custom derive code
• Understand how we use proc_macro, syn, and quote to achieve the custom derive logic
The aim of this video is to walk through important macros in the Rust standard library.
• Briefly review what we’ve learned about derive
• Walk through macros for formatting
• Walk through macros for introspection, including files, and more
The aim of this video is to walk through important macros in the Rust standard library.
• Walk through macros for conditional compiling
• Walk through macros for error handling
• Walk through macros that aid development like unimplemented, panic, or assert
How do we create a self-contained unit of module that can be reused? Using Cargo to create crates.
• Learn how the "cargo new" command creates crates
• Learn how a binary crate is structured
• Learn how a library crate is structured
How do we create namespace in crates to convey the structure of our code? Modules are built exactly for that.
• See how module definition works in one file
• Learn how we can split modules into files and folders
• Understand how the "pub" keyword controls the accessibility of modules
How do we reuse the modules we created or other people created? We can specify the dependency in Cargo.toml.
• Learn how Cargo.toml works
• How to use third-party or local crates
• How to include the crate in the code with "extern crate" and "use"
How do we publish our crate to the world? We need to put them on crates.io.
• Understand what crates.io is
• Understand the requirement for your crate to be published
• Demonstrate the procedure of publishing to crates.io
OpenCourser helps millions of learners each year. People visit us to learn workspace skills, ace their exams, and nurture their curiosity.
Our extensive catalog contains over 50,000 courses and twice as many books. Browse by search, by topic, or even by career interests. We'll match you to the right resources quickly.
Find this site helpful? Tell a friend about us.
We're supported by our community of learners. When you purchase or subscribe to courses and programs or purchase books, we may earn a commission from our partners.
Your purchases help us maintain our catalog and keep our servers humming without ads.
Thank you for supporting OpenCourser.