You've already forked comprehensive-rust
mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-11-28 00:39:01 +02:00
I asked Gemini to review the English for inconsistencies and grammar mistakes. This is the result. As a non-native speaker, it is hard for me to evaluate the finer details of this, so let me know if you would like to see changes (or even better: make them directly in the PR with the suggestion function).
1.9 KiB
1.9 KiB
minutes
| minutes |
|---|
| 10 |
Tuple Structs
If the field names are unimportant, you can use a tuple struct:
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):
struct PoundsOfForce(f64);
struct Newtons(f64);
fn compute_thruster_force() -> PoundsOfForce {
todo!("Ask a rocket scientist at NASA")
}
fn set_thruster_force(force: Newtons) {
// ...
}
fn main() {
let force = compute_thruster_force();
set_thruster_force(force);
}
- 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:
Newtonsin 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)orOddNumber(u32).
- The number is measured in some units:
- The newtype pattern is covered extensively in the "Idiomatic Rust" module.
- Demonstrate how to add a
f64value to aNewtonstype by accessing the single field in the newtype.- Rust generally avoids implicit conversions, like automatic unwrapping or
using booleans as integers.
- Operator overloading is discussed on Day 2 (Standard Library Traits).
- Rust generally avoids implicit conversions, like automatic unwrapping or
using booleans as integers.
- When a tuple struct has zero fields, the
()can be omitted. The result is a zero-sized type (ZST), of which there is only one value (the name of the type).- This is common for types that implement some behavior but have no data
(imagine a
NullReaderthat implements some reader behavior by always returning EOF).
- This is common for types that implement some behavior but have no data
(imagine a
- The example is a subtle reference to the Mars Climate Orbiter failure.