Concurrency and Memory Safe System Software Development in Rust

e81 cse 542s n.w
1 / 8
Embed
Share

Explore operator overloading, traits, arithmetic and bitwise operators, compound assignment operators, and comparisons in Rust programming for concurrency and memory-safe system software development. Learn how to implement and use operators effectively in Rust for efficient coding practices.

  • Rust Programming
  • Concurrency
  • Memory Safe
  • Operator Overloading
  • Software 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 Operator Overloading Department of Computer Science & Engineering Chris Gill cdgill@wustl.edu 1

  2. Operators and Traits I Rust operator expressions are shorthand for calls to methods from generic traits E.g., lhs * rhs for Mul::mul(lhs, rhs) Traits take, default, and/or define the types of function arguments and results, e.g., from Ch. 11 of the BOT textbook: pub trait Mul<RHS=Self> {type Output; fn mul(self, rhs: RHS) -> Self::Output; } 2 CSE 542S Concurrency and Memory Safe System Software Development

  3. Operators and Traits II So, overloading an operator in Rust is often as simple as implementing a given trait E.g., to overload * implement Mul accordingly The dereference operator * and access operator . are supported by Deref and DerefMut utility traits that we will cover in a later lecture and studio However, some operators can t be overloaded beyond types for which they re already defined E.g., the ? && || .. ..= & = operators Closures provide callable values via traits, so the function call operator is not overloaded either 3 CSE 542S Concurrency and Memory Safe System Software Development

  4. Arithmetic and Bitwise Operators These operators take their operands by value Rust usually avoids mixed-type operators, so all operand and result types are often the same However, the traits allow more flexibility if needed Unary operators - and ! provide negation (Neg trait) and complement (Not trait) respectively Binary operators provide overloadable arithmetic and bitwise operations similar to those in C++ But, some combinations like &str as the left-hand operand type of + are disallowed for efficiency 4 CSE 542S Concurrency and Memory Safe System Software Development

  5. Compound Assignment Operators Rust s compound assignment operators are shorthand for single function calls Not expressions of multiple function calls E.g., x += y for x.add_assign(y) directly Their traits also are independent of their corresponding binary operators traits E.g., implement std::ops::AddAssign for += but implement std::ops::Add for + 5 CSE 542S Concurrency and Memory Safe System Software Development

  6. Comparisons These operators have some common features They take their operands by reference, so they don t perform moves with non-copy types They default their RHS type parameter to Self and relax the requirement that the RHS type be sized Implement PartialEq trait for == and != I.e., implement its eq and ne methods Implement PartialOrd trait to overload the <, >, <=, and >= operators I.e., implement its partial_cmp method and its lt, le, gt, and ge methods 6 CSE 542S Concurrency and Memory Safe System Software Development

  7. Indexing Operators The Index and IndexMut traits support immutable vs. mutable uses of the [] indexing operator, respectively Both are parameterized by an indexing type Index also relaxes the requirement that its Output type is sized The IndexMut trait extends Index to overload [] operator for mutable indices I.e., implement index_mut method that takes &mut self (its index method takes &self) 7 CSE 542S Concurrency and Memory Safe System Software Development

  8. Studio 11 Examine how traits are used to overload different kinds of operators in Rust Try operator overloading for different types Studios 1 through 11 are due by 11:59pm on the night before Exam 1 Submit as soon as each is done so you get feedback and can resubmit any that may be marked incomplete 8 CSE 542S Concurrency and Memory Safe System Software Development

More Related Content