We may earn an affiliate commission when you visit our partners.
Course image
Packt Publishing

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.

Read more

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.

Enroll now

What's inside

Learning objectives

  • Write clean and reusable rust code for your applications
  • Use loop, map, filter and fold to avoid duplicated code
  • Understand generics and learn to use it to abstract algorithms for multiple data types
  • Define and enforce clear interface using traits
  • Work with macros and compiler plugins for metaprogramming
  • Explore how the standard library uses features such as generics, traits and macros
  • Structure your code with modules and crates and publish them online

Syllabus

Basics of Code Reuse

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

Read more

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

Traffic lights

Read about what's good
what should give you pause
and possible dealbreakers
Explores advanced features like traits, generics, and macros, which are essential for writing adaptable and maintainable Rust code
Covers structuring code with modules and crates, which is crucial for publishing and sharing reusable components in the Rust ecosystem
Examines metaprogramming with macros and compiler plugins, enabling developers to extend the language and reduce boilerplate code
Taught by an active member of the Rust community, who has contributed to Mozilla's Servo and Gecko browser engines
Requires setting up a Rust development environment locally, which may require some initial configuration for learners unfamiliar with Rust
Focuses on using Rust's standard library, which may require learners to familiarize themselves with the library's structure and conventions

Save this course

Create your own learning path. Save this course to your list so you can find it easily later.
Save

Reviews summary

Learner review of reusable rust code

According to learners, this course offers a solid foundation in Rust's advanced features like traits, generics, and macros, which are crucial for building reusable code. Students particularly praise the clear explanations and practical examples that help solidify complex concepts. The coverage of modules and publishing crates is also frequently mentioned as very helpful. While the course content is generally considered high quality and relevant, a few learners noted that certain topics, particularly macros, could benefit from more in-depth coverage or additional examples. The pace is generally seen as appropriate for those already familiar with Rust basics, but might be challenging for complete beginners. Overall, the sentiment is largely positive, highlighting its effectiveness in teaching Rust's reuse mechanisms.
Pace and content suit those past basics.
"This course is great if you already have a handle on Rust fundamentals and want to learn about reuse."
"I wouldn't recommend this as a first Rust course, but it's excellent for intermediate users."
"Assumes some prior knowledge of Rust syntax and concepts."
Helpful section on organizing and sharing code.
"The part about structuring code with modules and crates was very practical and immediately applicable."
"Learning how to publish to crates.io was a valuable takeaway."
"The organization section tied everything together nicely for building sharable libraries."
Includes good, practical coding examples.
"The hands-on coding examples really helped solidify my understanding of the concepts."
"I appreciated the practical demonstrations of using traits and generics in real-world scenarios."
"The examples were well-chosen and illustrated the concepts effectively."
Instructor explains difficult concepts clearly.
"The instructor did a great job of making potentially difficult concepts like lifetimes and ownership (in the context of generics) understandable."
"Explanations were concise and easy to follow."
"I found the teaching style very effective for absorbing new information."
Effectively teaches Rust traits, generics, and macros.
"This course provided me with a solid foundation in Rust traits, generics, and macros for building reusable code."
"I found the explanations of traits and generics particularly clear and helpful."
"The way the course broke down complex topics like macros was really effective for my understanding."
"Covers essential Rust features for writing modular and reusable code."
Certain areas could use more detailed explanation.
"I felt the section on macros, while an introduction, could have gone into a bit more detail or provided more complex examples."
"Could use more coverage on advanced macro techniques or edge cases."
"Some parts felt a bit rushed, particularly when explaining the nuances of trait objects."

Activities

Be better prepared before your course. Deepen your understanding during and after it. Supplement your coursework and achieve mastery of the topics covered in Building Reusable Code with Rust with these activities:
Review Basic Rust Syntax
Solidify your understanding of fundamental Rust syntax to better grasp the more advanced concepts of code reuse.
Browse courses on Rust
Show steps
  • Review data types, variables, and control flow in Rust documentation.
  • Write small programs using basic syntax to reinforce understanding.
Read 'The Rust Programming Language'
Gain a deeper understanding of Rust's core principles and syntax, which are essential for building reusable code.
Show steps
  • Read the chapters relevant to generics, traits, and macros.
  • Work through the examples provided in the book.
Implement common data structures using generics
Practice using generics to implement data structures like linked lists or binary trees to solidify your understanding.
Show steps
  • Choose a data structure to implement.
  • Write the code using generics to handle different data types.
  • Test the implementation with various data types.
Four other activities
Expand to see all activities and additional details
Show all seven activities
Write a blog post on Rust traits
Explain the concept of traits in Rust and how they enable code reuse through polymorphism.
Show steps
  • Research and gather information on Rust traits.
  • Write a clear and concise explanation of traits with examples.
  • Publish the blog post on a platform like Medium or personal blog.
Contribute to a Rust project
Apply your knowledge of reusable code by contributing to an open-source Rust project.
Show steps
  • Find a Rust project on GitHub that interests you.
  • Identify an issue or feature to work on.
  • Submit a pull request with your changes.
Build a reusable library crate
Create a library crate that provides reusable components or functions for other Rust projects.
Show steps
  • Define the scope and functionality of the library.
  • Implement the library using generics, traits, and macros.
  • Write documentation and tests for the library.
  • Publish the crate to crates.io.
Read 'Programming Rust'
Explore advanced Rust concepts and techniques to further enhance your ability to write reusable and efficient code.
Show steps
  • Read the chapters on advanced topics like unsafe Rust and concurrency.
  • Experiment with the examples provided in the book.

Career center

Learners who complete Building Reusable Code with Rust will develop knowledge and skills that may be useful to these careers:
Software Engineer
A software engineer is often responsible for developing, testing, and maintaining software applications. This course helps a software engineer write clean, reusable code using Rust, a skill that is very important in a professional setting, as they often need to collaborate with other engineers and avoid duplicating code. The course teaches how to use advanced features such as traits, generics, and macros to achieve higher-level code reuse, which minimizes redundancies and promotes maintainable and scalable software solutions. The course's section on modules and crates is relevant, as it teaches how to structure code for distribution and collaboration.
Systems Programmer
A systems programmer specializes in low-level programming, often working on operating systems, device drivers, and other system software. This course may be useful in that it introduces the idea of writing safe, reusable code, a vital need for a systems programmer. By learning to implement features like loops, maps, filters, and folds, one can avoid code duplication and optimize performance. The course also covers the use of modules and crates, which is essential for organizing and reusing system-level components, a typical activity for a systems programmer.
Embedded Systems Engineer
An embedded systems engineer designs software for embedded devices such as microcontrollers and sensors. This course may be useful, as embedded systems often have resource constraints, meaning the ability to write efficient, reusable code is critical. The course's focus on avoiding code duplication using features like loops, traits, and generics will allow an embedded systems engineer to write clean code. Furthermore, understanding how to organize code into modules and crates can be helpful in managing complexity in embedded projects, where code bases can grow large.
Blockchain Developer
A blockchain developer is concerned with creating decentralized applications and networks using blockchain technology. This course may be useful, as blockchain projects often require robust, efficient, and reusable code for smart contracts and core protocols. The course’s material on using Rust to write safe and reusable code is very useful, as it helps avoid vulnerabilities. The course's coverage of traits, generics, and macros will allow a blockchain developer to write flexible and maintainable blockchain solutions. The modularity principles discussed in the course will also help organize complex blockchain projects.
Game Developer
A game developer uses code to create interactive gaming experiences. This course may be useful for a game developer. The material on writing reusable code can help when developing game engines and core gameplay mechanics. Specifically, the course’s training on Rust's advanced features like generics and traits will enable a game developer to create flexible and adaptable game systems. The course also covers modules and crates, valuable skills to help a game developer organize large game codebases and share components within a development team.
Operating Systems Developer
An operating systems developer contributes to the development of the core software that manages computer hardware and software resources. This course may be of use to an operating systems developer, as it focuses on creating reusable components with Rust, which is itself an operating system level language. An operating systems developer may find that the course emphasis on traits and macros could help them build flexible and maintainable operating system components. The sections on modules and crates could be helpful when working on big projects with many files.
Compiler Engineer
A compiler engineer works on the design and implementation of compilers and language tools. This course may be useful due to Rust’s focus on metaprogramming (writing code that manipulates code). A compiler engineer might find the course’s sections on macros and compiler plugins very interesting. The course also covers how to use traits, generics, and modules, which are also relevant to compiler design (which involves a lot of code that is generic and needs to be reused). A course such as this, on how to organize code well, will benefit a compiler engineer.
Robotics Engineer
A robotics engineer is engaged in creating intelligent machines that can perform tasks autonomously or semi-autonomously. This course may be useful because robotics often requires efficient, reliable, and reusable software components. The course’s focus on safe and reusable code with Rust is helpful. The course's coverage of traits and generics will allow a robotics engineer to develop flexible robot control software. The modularity concepts taught may help with structuring the code for complex robotic systems.
Data Engineer
A data engineer is concerned with building and maintaining data pipelines and infrastructure. This course may be useful to a data engineer, as it teaches how to write reusable code that can be used in data processing and transformation tools. The course’s material on traits, generics, and macros will allow a data engineer to create adaptable and efficient data processing frameworks. The modularity principles taught would also assist in managing complexity in large data projects. While data engineers often work with Python and other languages, Rust is finding use in the data domain.
DevOps Engineer
A DevOps engineer focuses on integrating development and operations to enhance software deployment processes. A DevOps engineer may find this course useful. The principles of reusable code, as taught in this course, are handy in writing automation scripts and infrastructure-as-code configurations. The course's training in using modules and crates would benefit a DevOps engineer in standardizing and sharing deployment scripts and configurations across many environments. Further, Rust's performance and safety characteristics could be useful for building infrastructure tools.
Algorithm Developer
An algorithm developer designs and implements algorithms for various computational tasks. This course may be useful because the ability to write reusable, efficient code is important for algorithm implementation. The course's coverage of generics and traits would allow an algorithm developer to create adaptable implementations for different types. Further, an understanding of loops, map, filter, and fold can help them optimize and refine algorithmic implementations. The modularity concepts may help organize and maintain code libraries of various algorithms.
Quantitative Analyst
A quantitative analyst, also known as a quant, develops mathematical and statistical models for financial markets. This course may be useful for quants who use programming to develop their trading algorithms. The course’s concepts of reusable code using generics and traits would allow them to create abstract trading models. The performance orientation of Rust would be useful for quants in developing high-frequency trading algorithms. Further, a quant can use the modularity tools of Rust to structure financial models.
Security Engineer
A security engineer works to protect computer systems and networks from threats. This course may be useful in that Rust can be used to build secure and reliable security tools. The course’s material on writing safe, reusable code with Rust is important for a security engineer. Specifically, the course's training on how to use traits and macros would enable them to build flexible tools. The course's discussion of modules and crates could be relevant in organizing the code for large projects.
Database Developer
A database developer is in charge of designing, implementing, and maintaining databases. This course may be useful to a database developer who uses Rust for database engine development. The ability to write reusable code, using tools like traits, generics, and macros as taught in the course would help build components that are adaptable. The course’s training in structuring code will also assist with the work of a database developer who needs to deal with many parts of a project.
Research Scientist
A research scientist engages in scientific research, often involving data analysis and programming. While research scientists typically use languages such as Python, this course may be useful, as it teaches efficient, reusable coding practices, which may be useful for performance-critical research tasks. The course’s material on how to avoid code duplication may be beneficial for research projects. This course may be helpful to a research scientist who uses languages other than Python, or who develops their own tools and libraries.

Reading list

We've selected two books that we think will supplement your learning. Use these to develop background knowledge, enrich your coursework, and gain a deeper understanding of the topics covered in Building Reusable Code with Rust.
Is the official guide to Rust and provides a comprehensive overview of the language. It covers all the core concepts, including ownership, borrowing, and concurrency. It is particularly useful for understanding the underlying principles behind Rust's features for code reuse. This book is commonly used as a textbook at academic institutions.
Provides a practical, hands-on approach to learning Rust. It covers advanced topics such as unsafe Rust and concurrency. It is valuable as additional reading to deepen your understanding of Rust's capabilities. This book is commonly used by industry professionals.

Share

Help others find this course page by sharing it with your friends and followers:

Similar courses

Similar courses are unavailable at this time. Please try again later.
Our mission

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.

Affiliate disclosure

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.

© 2016 - 2025 OpenCourser