From d8cbe4eedb77bae3db9eff87b1238e7e23f6ae92 Mon Sep 17 00:00:00 2001 From: "Havvy (Ryan Scheel)" Date: Sat, 13 Feb 2021 22:34:43 -0800 Subject: [PATCH 1/3] Remove enum variant expression page It is quite redundant with the (IMO badly named) struct expression. Perhaps we should rename it to something else? "Named value expression"? --- src/SUMMARY.md | 1 - src/attributes/type_system.md | 3 +- src/const_eval.md | 2 -- src/expressions.md | 6 +--- src/expressions/block-expr.md | 3 +- src/expressions/enum-variant-expr.md | 47 ---------------------------- src/expressions/struct-expr.md | 10 +++--- src/items/enumerations.md | 2 +- src/types/enum.md | 5 ++- 9 files changed, 12 insertions(+), 67 deletions(-) delete mode 100644 src/expressions/enum-variant-expr.md diff --git a/src/SUMMARY.md b/src/SUMMARY.md index e89fe0f08..783c647b4 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -56,7 +56,6 @@ - [Array and index expressions](expressions/array-expr.md) - [Tuple and index expressions](expressions/tuple-expr.md) - [Struct expressions](expressions/struct-expr.md) - - [Enum variant expressions](expressions/enum-variant-expr.md) - [Call expressions](expressions/call-expr.md) - [Method call expressions](expressions/method-call-expr.md) - [Field access expressions](expressions/field-expr.md) diff --git a/src/attributes/type_system.md b/src/attributes/type_system.md index 97938f2d7..729069d26 100644 --- a/src/attributes/type_system.md +++ b/src/attributes/type_system.md @@ -64,7 +64,7 @@ Non-exhaustive types cannot be constructed outside of the defining crate: - Non-exhaustive variants ([`struct`][struct] or [`enum` variant][enum]) cannot be constructed with a [_StructExpression_] \(including with [functional update syntax]). -- [`enum`][enum] instances can be constructed in an [_EnumerationVariantExpression_]. +- [`enum`][enum] instances can be constructed. ```rust,ignore @@ -129,7 +129,6 @@ match message { Non-exhaustive types are always considered inhabited in downstream crates. -[_EnumerationVariantExpression_]: ../expressions/enum-variant-expr.md [_MetaWord_]: ../attributes.md#meta-item-attribute-syntax [_StructExpression_]: ../expressions/struct-expr.md [_StructPattern_]: ../patterns.md#struct-patterns diff --git a/src/const_eval.md b/src/const_eval.md index 159f0a768..4f6716530 100644 --- a/src/const_eval.md +++ b/src/const_eval.md @@ -27,7 +27,6 @@ to be run. * [Tuple expressions]. * [Array expressions]. * [Struct] expressions. -* [Enum variant] expressions. * [Block expressions], including `unsafe` blocks. * [let statements] and thus irrefutable [patterns], including mutable bindings * [assignment expressions] @@ -117,7 +116,6 @@ Conversely, the following are possible in a const function, but not in a const c [dereference operator]: expressions/operator-expr.md#the-dereference-operator [destructors]: destructors.md [enum discriminants]: items/enumerations.md#custom-discriminant-values-for-fieldless-enumerations -[enum variant]: expressions/enum-variant-expr.md [expression statements]: statements.md#expression-statements [expressions]: expressions.md [field]: expressions/field-expr.md diff --git a/src/expressions.md b/src/expressions.md index c2702a342..09b433365 100644 --- a/src/expressions.md +++ b/src/expressions.md @@ -18,7 +18,6 @@ >       | [_TupleExpression_]\ >       | [_TupleIndexingExpression_]\ >       | [_StructExpression_]\ ->       | [_EnumerationVariantExpression_]\ >       | [_CallExpression_]\ >       | [_MethodCallExpression_]\ >       | [_FieldExpression_]\ @@ -103,7 +102,6 @@ evaluate them conditionally as described on their respective pages. * Tuple expression * Tuple index expression * Struct expression -* Enumeration variant expression * Call expression * Method call expression * Field expression @@ -259,7 +257,7 @@ a few specific cases: * Before an expression used as a [statement]. * Elements of [array expressions], [tuple expressions], [call expressions], - and tuple-style [struct] and [enum variant] expressions. + and tuple-style [struct] expressions. @@ -178,7 +178,6 @@ fn is_unix_platform() -> bool { [`while`]: loop-expr.md#predicate-loops [array expressions]: array-expr.md [call expressions]: call-expr.md -[enum variant]: enum-variant-expr.md [function]: ../items/functions.md [inner attributes]: ../attributes.md [method]: ../items/associated-items.md#methods diff --git a/src/expressions/enum-variant-expr.md b/src/expressions/enum-variant-expr.md deleted file mode 100644 index 961d654ce..000000000 --- a/src/expressions/enum-variant-expr.md +++ /dev/null @@ -1,47 +0,0 @@ -# Enumeration Variant expressions - -> **Syntax**\ -> _EnumerationVariantExpression_ :\ ->       _EnumExprStruct_\ ->    | _EnumExprTuple_\ ->    | _EnumExprFieldless_ -> -> _EnumExprStruct_ :\ ->    [_PathInExpression_] `{` _EnumExprFields_? `}` -> -> _EnumExprFields_ :\ ->       _EnumExprField_ (`,` _EnumExprField_)\* `,`? -> -> _EnumExprField_ :\ ->       [IDENTIFIER]\ ->    | ([IDENTIFIER] | [TUPLE_INDEX]) `:` [_Expression_] -> -> _EnumExprTuple_ :\ ->    [_PathInExpression_] `(`\ ->       ( [_Expression_] (`,` [_Expression_])\* `,`? )?\ ->    `)` -> -> _EnumExprFieldless_ : [_PathInExpression_] - -Enumeration variants can be constructed similarly to [structs], using a path to an enum -variant instead of to a struct: - -```rust -# enum Message { -# Quit, -# WriteString(String), -# Move { x: i32, y: i32 }, -# } -let q = Message::Quit; -let w = Message::WriteString("Some string".to_string()); -let m = Message::Move { x: 50, y: 200 }; -``` - -Enum variant expressions have the same syntax, behavior, and restrictions as [struct -expressions][structs], except they do not support base update with the `..` syntax. - -[IDENTIFIER]: ../identifiers.md -[TUPLE_INDEX]: ../tokens.md#tuple-index -[_Expression_]: ../expressions.md -[_PathInExpression_]: ../paths.md#paths-in-expressions -[structs]: struct-expr.md diff --git a/src/expressions/struct-expr.md b/src/expressions/struct-expr.md index 40708d98d..5179046cf 100644 --- a/src/expressions/struct-expr.md +++ b/src/expressions/struct-expr.md @@ -27,8 +27,8 @@ > > _StructExprUnit_ : [_PathInExpression_] -A _struct expression_ creates a struct or union value. -It consists of a path to a [struct] or [union] item followed by the values for the fields of the item. +A *struct expression* creates a struct, enum, or union value. +It consists of a path to a [struct], [enum], or [union] item followed by the values for the fields of the item. There are three forms of struct expressions: struct, tuple, and unit. The following are examples of struct expressions: @@ -52,7 +52,7 @@ some_fn::(Cookie); A struct expression with fields enclosed in curly braces allows you to specify the value for each individual field in any order. The field name is separated from its value with a colon. -A value of a [union] type can also be created using this syntax, except that it must specify exactly one field. +A value of a [union] type can only be created using this syntax, and it must specify exactly one field. ## Functional update syntax @@ -101,7 +101,8 @@ Point3d { x, y: y_value, z }; ## 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: +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: ```rust struct Position(i32, i32, i32); @@ -134,6 +135,7 @@ let b = Gamma{}; // Exact same value as `a`. [_PathInExpression_]: ../paths.md#paths-in-expressions [attributes on block expressions]: block-expr.md#attributes-on-block-expressions [call expression]: call-expr.md +[enum]: ../items/enumerations.md [if let]: if-expr.md#if-let-expressions [if]: if-expr.md#if-expressions [loop]: loop-expr.md diff --git a/src/items/enumerations.md b/src/items/enumerations.md index 8248a47db..28d3ba873 100644 --- a/src/items/enumerations.md +++ b/src/items/enumerations.md @@ -25,7 +25,7 @@ > _EnumItemDiscriminant_ :\ >    `=` [_Expression_] -An *enumeration*, also referred to as *enum* is a simultaneous definition of a +An *enumeration*, also referred to as an *enum*, is a simultaneous definition of a nominal [enumerated type] as well as a set of *constructors*, that can be used to create or pattern-match values of the corresponding enumerated type. diff --git a/src/types/enum.md b/src/types/enum.md index 8f69dbad8..8f81fb1a5 100644 --- a/src/types/enum.md +++ b/src/types/enum.md @@ -7,8 +7,7 @@ An [`enum` item] declares both the type and a number of *variants*, each of which is independently named and has the syntax of a struct, tuple struct or unit-like struct. -New instances of an `enum` can be constructed in an [enumeration variant -expression]. +New instances of an `enum` can be constructed with a [struct expression]. Any `enum` value consumes as much memory as the largest variant for its corresponding `enum` type, as well as the size needed to store a discriminant. @@ -20,4 +19,4 @@ named reference to an [`enum` item]. ML, or a *pick ADT* in Limbo. [`enum` item]: ../items/enumerations.md -[enumeration variant expression]: ../expressions/enum-variant-expr.md +[struct expression]: ../expressions/struct-expr.md From 4471b0efbc1d80074de0305b0c5df322f8419a5a Mon Sep 17 00:00:00 2001 From: "Havvy (Ryan Scheel)" Date: Sun, 14 Feb 2021 12:26:59 -0800 Subject: [PATCH 2/3] Address review comments w.r.t. remove enum variant expr --- book.toml | 3 +++ src/expressions/struct-expr.md | 7 +++---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/book.toml b/book.toml index b1335de77..d57c9a170 100644 --- a/book.toml +++ b/book.toml @@ -6,3 +6,6 @@ author = "The Rust Project Developers" [output.html] additional-css = ["theme/reference.css"] git-repository-url = "https://github.com/rust-lang/reference/" + +[output.html.redirect] +"/expressions/enum-variant-expr.html" = "struct-expr.html" \ No newline at end of file diff --git a/src/expressions/struct-expr.md b/src/expressions/struct-expr.md index 5179046cf..31689bd68 100644 --- a/src/expressions/struct-expr.md +++ b/src/expressions/struct-expr.md @@ -28,7 +28,7 @@ > _StructExprUnit_ : [_PathInExpression_] A *struct expression* creates a struct, enum, or union value. -It consists of a path to a [struct], [enum], or [union] item followed by the values for the fields of the item. +It consists of a path to a [struct], [enum variant], or [union] item followed by the values for the fields of the item. There are three forms of struct expressions: struct, tuple, and unit. The following are examples of struct expressions: @@ -101,8 +101,7 @@ Point3d { x, y: y_value, z }; ## 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: +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: ```rust struct Position(i32, i32, i32); @@ -135,7 +134,7 @@ let b = Gamma{}; // Exact same value as `a`. [_PathInExpression_]: ../paths.md#paths-in-expressions [attributes on block expressions]: block-expr.md#attributes-on-block-expressions [call expression]: call-expr.md -[enum]: ../items/enumerations.md +[enum variant]: ../items/enumerations.md [if let]: if-expr.md#if-let-expressions [if]: if-expr.md#if-expressions [loop]: loop-expr.md From d91074bf26f4cd8f1d36563f298da81e805b09ab Mon Sep 17 00:00:00 2001 From: "Havvy (Ryan Scheel)" Date: Sun, 14 Feb 2021 12:29:32 -0800 Subject: [PATCH 3/3] Say Field Record Update only works for structs. This is a minimal change, as I'd like to make more massive changes in a separate PR touching on most of the page. --- src/expressions/struct-expr.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/expressions/struct-expr.md b/src/expressions/struct-expr.md index 31689bd68..ad9fb76f8 100644 --- a/src/expressions/struct-expr.md +++ b/src/expressions/struct-expr.md @@ -56,7 +56,7 @@ A value of a [union] type can only be created using this syntax, and it must spe ## Functional update syntax -A struct expression can terminate with the syntax `..` followed by an expression to denote a functional update. +A struct expression that constructs a value of a struct type can terminate with the syntax `..` followed by an expression to denote a functional update. The expression following `..` (the base) must have the same struct type as the new struct type being formed. The entire expression uses the given values for the fields that were specified and moves or copies the remaining fields from the base expression.