2023-11-29 10:39:24 -05:00
|
|
|
---
|
|
|
|
minutes: 10
|
|
|
|
---
|
|
|
|
|
|
|
|
<!-- NOTES:
|
|
|
|
Tuple structs, newtype wrappers, unit-like structs, including initialization syntax
|
|
|
|
-->
|
2023-12-31 00:15:07 +01:00
|
|
|
|
2022-12-21 16:36:30 +01:00
|
|
|
# Tuple Structs
|
|
|
|
|
|
|
|
If the field names are unimportant, you can use a tuple struct:
|
|
|
|
|
|
|
|
```rust,editable
|
|
|
|
struct Point(i32, i32);
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let p = Point(17, 23);
|
|
|
|
println!("({}, {})", p.0, p.1);
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
This is often used for single-field wrappers (called newtypes):
|
|
|
|
|
|
|
|
```rust,editable,compile_fail
|
2023-05-05 08:57:44 +01:00
|
|
|
struct PoundsOfForce(f64);
|
2022-12-21 16:36:30 +01:00
|
|
|
struct Newtons(f64);
|
|
|
|
|
2023-05-05 08:57:44 +01:00
|
|
|
fn compute_thruster_force() -> PoundsOfForce {
|
2022-12-21 16:36:30 +01:00
|
|
|
todo!("Ask a rocket scientist at NASA")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_thruster_force(force: Newtons) {
|
|
|
|
// ...
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let force = compute_thruster_force();
|
|
|
|
set_thruster_force(force);
|
|
|
|
}
|
|
|
|
```
|
2023-01-23 11:38:37 +00:00
|
|
|
|
|
|
|
<details>
|
|
|
|
|
2023-12-31 00:15:07 +01:00
|
|
|
- Newtypes are a great way to encode additional information about the value in a
|
|
|
|
primitive type, for example:
|
|
|
|
- The number is measured in some units: `Newtons` in the example above.
|
|
|
|
- The value passed some validation when it was created, so you no longer have
|
|
|
|
to validate it again at every use: `PhoneNumber(String)` or
|
|
|
|
`OddNumber(u32)`.
|
|
|
|
- Demonstrate how to add a `f64` value to a `Newtons` type by accessing the
|
|
|
|
single field in the newtype.
|
|
|
|
- Rust generally doesn’t like inexplicit things, like automatic unwrapping or
|
|
|
|
for instance using booleans as integers.
|
|
|
|
- Operator overloading is discussed on Day 3 (generics).
|
|
|
|
- The example is a subtle reference to the
|
|
|
|
[Mars Climate Orbiter](https://en.wikipedia.org/wiki/Mars_Climate_Orbiter)
|
|
|
|
failure.
|
2023-11-29 10:39:24 -05:00
|
|
|
|
2023-01-23 11:38:37 +00:00
|
|
|
</details>
|