From 41f485a0be2d9676ea6bc0ae64ca738e9ed08c97 Mon Sep 17 00:00:00 2001 From: Waffle Lapkin Date: Sun, 20 Apr 2025 15:42:32 +0200 Subject: [PATCH 1/2] document that unit&typle structure expr can be qualified + simplify/clarify the grammar --- src/expressions/struct-expr.md | 35 +++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/src/expressions/struct-expr.md b/src/expressions/struct-expr.md index 79a3a2e20..93bbf4ff2 100644 --- a/src/expressions/struct-expr.md +++ b/src/expressions/struct-expr.md @@ -23,12 +23,9 @@ StructExprField -> StructBase -> `..` Expression -StructExprTuple -> - PathInExpression `(` - ( Expression (`,` Expression)* `,`? )? - `)` +StructExprTuple -> CallExpression -StructExprUnit -> PathInExpression +StructExprUnit -> PathExpression ``` r[expr.struct.intro] @@ -129,6 +126,20 @@ let c = Position; // `c` is a function that takes 3 arguments. let pos = c(8, 6, 7); // Creates a `Position` value. ``` +> [!NOTE] +> While the grammar permits qualified paths, the last segment can't be a type alias: +> +> ```rust +> trait Tr { type T; } +> impl Tr for T { type T = T; } +> +> struct Tuple(); +> enum Enum { Tuple() } +> +> // ::T(); // causes an error -- `::T` is a type, not a value +> ::T::Tuple(); // OK +> ``` + r[expr.struct.unit] ## Unit struct expression @@ -142,6 +153,20 @@ let a = Gamma; // Gamma unit value. let b = Gamma{}; // Exact same value as `a`. ``` +> [!NOTE] +> While the grammar permits qualified paths, the last segment can't be a type alias: +> +> ```rust +> trait Tr { type T; } +> impl Tr for T { type T = T; } +> +> struct Unit; +> enum Enum { Unit } +> +> // ::T; // causes an error -- `::T` is a type, not a value +> ::T::Unit; // OK +> ``` + [call expression]: call-expr.md [enum variant]: ../items/enumerations.md [if let]: if-expr.md#if-let-expressions From f025b982e6d5b4e35674b7f4e2853f43f4ef6830 Mon Sep 17 00:00:00 2001 From: Waffle Lapkin Date: Sun, 20 Apr 2025 22:40:33 +0200 Subject: [PATCH 2/2] add examples of constructing enum variants --- src/expressions/struct-expr.md | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/expressions/struct-expr.md b/src/expressions/struct-expr.md index 93bbf4ff2..986679a0a 100644 --- a/src/expressions/struct-expr.md +++ b/src/expressions/struct-expr.md @@ -41,11 +41,13 @@ The following are examples of struct expressions: # struct TuplePoint(f64, f64); # mod game { pub struct User<'a> { pub name: &'a str, pub age: u32, pub score: usize } } # struct Cookie; fn some_fn(t: T) {} +# enum Enum { Variant {} } Point {x: 10.0, y: 20.0}; NothingInMe {}; TuplePoint(10.0, 20.0); TuplePoint { 0: 10.0, 1: 20.0 }; // Results in the same value as the above line let u = game::User {name: "Joe", age: 35, score: 100_000}; +Enum::Variant {}; some_fn::(Cookie); ``` @@ -116,14 +118,19 @@ Point3d { x, y: y_value, z }; r[expr.struct.tuple] ## Tuple struct expression -A struct expression with fields enclosed in parentheses constructs a tuple struct. -Though it is listed here as a specific expression for completeness, it is equivalent to a [call expression] to the tuple struct's constructor. For example: +A struct expression with fields enclosed in parentheses constructs a tuple struct or a tuple variant of an enum. +Though it is listed here as a specific expression for completeness, it is equivalent to a [call expression] to the tuple struct's (enum tuple variant's) constructor. For example: ```rust struct Position(i32, i32, i32); Position(0, 0, 0); // Typical way of creating a tuple struct. let c = Position; // `c` is a function that takes 3 arguments. let pos = c(8, 6, 7); // Creates a `Position` value. + +enum Version { Triple(i32, i32, i32) }; +Version::Triple(0, 0, 0); +let f = Version::Triple; +let ver = f(8, 6, 7); ``` > [!NOTE] @@ -143,14 +150,18 @@ let pos = c(8, 6, 7); // Creates a `Position` value. r[expr.struct.unit] ## Unit struct expression -A unit struct expression is just the path to a unit struct item. -This refers to the unit struct's implicit constant of its value. -The unit struct value can also be constructed with a fieldless struct expression. For example: +A unit struct expression is just the path to a unit struct item or unit variant of an enum. +This refers to the unit struct's (enum variant's) implicit constant of its value. +The unit struct or a unit variant of an enum value can also be constructed with a fieldless struct expression. For example: ```rust struct Gamma; let a = Gamma; // Gamma unit value. let b = Gamma{}; // Exact same value as `a`. + +enum ColorSpace { Oklch } +let c = ColorSpace::Oklch; +let d = ColorSpace::Oklch {}; ``` > [!NOTE]