home—lectures—recipe—hws—exams—D2L—zoom (snow day)
structs intro
Reminder:
- String = tuple (ptr, len, capacity)
- Vector = tuple (ptr, len, capacity)
(but array of any type, not just char)
array-slice T[] = Struct(ptr,len).
&str is essentially the same.
Box = Struct(ptr)
Structs
Like a Java object, but fields-only.
(See Rust Book §§5.1-5.2)
- syntax for declaring, and making. (see Rust Book)
- mut: applies to whole struct, not individual fields!
- a shortcut syntax for copying from another struct.
- weird/optional a shortcut: if you have a variable that IS the fieldname, you can just mention the var (w/o colon).
- A struct must own its data (in order to avoid lifetimes).
Hence, we use String not &str for our fields.
- Tuples:
- like structs, but you don't name the fields.
- access fields with .0, .1, .2 etc.
- Best for: returning two or three things from a function.
- Tuples do NOT own their data.
home—lectures—recipe—hws—exams—D2L—zoom (snow day)
 This page licensed CC-BY 4.0 Ian Barland Page last generated | Please mail any suggestions (incl. typos, broken links) to ibarland radford.edu |
 |