1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-04-22 23:58:39 +02:00
comprehensive-rust/src/structs/tuple-structs.md

34 lines
588 B
Markdown
Raw Normal View History

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
struct PoundOfForce(f64);
struct Newtons(f64);
fn compute_thruster_force() -> PoundOfForce {
todo!("Ask a rocket scientist at NASA")
}
fn set_thruster_force(force: Newtons) {
// ...
}
fn main() {
let force = compute_thruster_force();
set_thruster_force(force);
}
```