src/user-defined-types/tuple-structs.md
If the field names are unimportant, you can use a tuple struct:
# // Copyright 2023 Google LLC
# // SPDX-License-Identifier: Apache-2.0
#
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):
# // Copyright 2023 Google LLC
# // SPDX-License-Identifier: Apache-2.0
#
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);
}
Newtons in the example above.PhoneNumber(String) or
OddNumber(u32).f64 value to a Newtons type by accessing the
single field in the newtype.
() can be omitted. The result is a
zero-sized type (ZST), of which there is only one value (the name of the
type).
NullReader that implements some reader behavior by always
returning EOF).