
Structs in Rust: Overview and Implementation
Learn about structs in Rust, which organize related data, methods, and constants into types. Understand the different kinds of structs available and how to work with associated functions, constants, and generic structs. Explore deriving traits for structs and practical exercises for declaring and using different struct types.
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
E81 CSE 542S: Concurrency and Memory Safe System Software Development Structs Department of Computer Science & Engineering Chris Gill cdgill@wustl.edu 1
Structs in Rust Rust structs collect closely related data, methods, and constants into types Data layout is specified in the declaration There are three distinct kinds of structs Named-field structs whose elements have names that are used to access them Elements of tuple-like structs are indexed Unit-like structs have no elements at all 2 CSE 542S Concurrency and Memory Safe System Software Development
Associated Functions and Constants An impl block is used to associate functions and constants with struct data As with struct data, are private by default Methods have one extra argument First argument of a method is self, may be an instance, reference, Box, Rc, or Arc Associated functions (e.g., new) don t have self so they are defined at the level of the struct type rather than of an instance 3 CSE 542S Concurrency and Memory Safe System Software Development
Generic Structs Generic struct declarations & impl blocks can take type, lifetime, constant params Analogous to C++ template signatures Associated function calls can supply specific values for those explicitly Uses the turbofish notation: ::<> E.g., let mut v = Vec::<String>::new(); 4 CSE 542S Concurrency and Memory Safe System Software Development
Deriving Traits for Structs Many useful operations are not available by default when you declare a struct If default generic behavior is ok, can ask the compiler to supply them for you A #[derive] attribute causes specified traits to be derived for the struct type E.g., Copy, Clone, Debug, PartialEq, PartialOrd, etc. (see BOT Chapter 13) 5 CSE 542S Concurrency and Memory Safe System Software Development
Studio 8 Practice declaring and using different structs Explore how to associate functions and methods, and how to derive traits automatically Examine how generic parameters can be added 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 6 CSE 542S Concurrency and Memory Safe System Software Development