Utility Traits and Language Extension in Rust

e81 cse 542s l.w
1 / 7
Embed
Share

Explore the three kinds of utility traits in Rust that enhance the language by extending it for new types, constraining types, and establishing design vocabulary. Learn about language extension traits like Drop, Deref, and From that enable advanced functionalities for Rust developers. Discover how marker traits and public vocabulary traits play a crucial role in Rust development to restrict generic types and create default initializations.

  • Rust Programming
  • Utility Traits
  • Language Extension
  • Marker Traits
  • Rust Development

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. E81 CSE 542S: Concurrency and Memory Safe System Software Development Utility Traits Department of Computer Science & Engineering Chris Gill cdgill@wustl.edu 1

  2. Three Kinds of Utility Traits in Rust Traits that help extend the Rust language Especially for new types that you create Marker traits that help constrain types Especially when dealing with generics Traits that establish a design vocabulary E.g., to solve common problems consistently 2 CSE 542S Concurrency and Memory Safe System Software Development

  3. Language Extension Traits The Drop trait supports cleanup code that needs to run when a value is dropped Analogous to a destructor in C++ Runs Drop::drop method on initialized value The Deref and DerefMut traits govern how dereferencing operators * and . behave E.g., direct invocation of methods on Rc<T> The From and Into traits allow symmetrical conversions between your type and others May rely heavily on compiler s type inference 3 CSE 542S Concurrency and Memory Safe System Software Development

  4. Restricting Generic Types Marker traits let code restrict or annotate which types are used for a generic parameter E.g., impl<T: SomeTrait> SomeStruct<T> {...} E.g., impl<T: ?ATrait> AStruct<T> {...} Each marker identifies a particular set of types E.g., use Sized for types whose size is known at run-time (or to indicate that s questionable) E.g., use Copyfor copy types (that aren t moved) 4 CSE 542S Concurrency and Memory Safe System Software Development

  5. Public Vocabulary Traits The Defaulttrait supports a type s default initialization (like C++ default constructor) The AsRef, AsMut, Borrow, and BorrowMut traits support different borrowing semantics The TryFrom and TryInto traits are for conversions that may lose info or fail The ToOwned and Clone traits are used to create types from same or other types 5 CSE 542S Concurrency and Memory Safe System Software Development

  6. Clone on Write (Cow) Enum Many languages support a form of the Copy on Write idiom for shared values Defer duplication until a write forces that Also seen and supported in some operating systems Rust supports Clone on Write with enum Cow Its Borrowed label carries a shared reference Its Owned label carries an owned value produced via ToOwned or Clone from the original value 6 CSE 542S Concurrency and Memory Safe System Software Development

  7. Studio 12 Examine different utility traits and how to implement them for your own types Explore semantics of those traits, and how they can be used w.r.t. program behavior Studios 12 through 20 are due by 11:59pm on the night before Exam 2 Submit as soon as each is done so you get feedback and can resubmit any that may be marked incomplete 7 CSE 542S Concurrency and Memory Safe System Software Development

More Related Content