diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2021-11-09 19:00:41 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-11-09 19:00:41 +0100 |
| commit | 610b4e503ccc5cb6d4ef98bb5016ba42eaf94522 (patch) | |
| tree | c8db226e573691cc2ffcd379127dc226d48bd564 /src/doc | |
| parent | fd74c93403c455187c343f3828274824addc9881 (diff) | |
| parent | 926892ddc0b75b50f5d0a3483d829e501aa8e895 (diff) | |
| download | rust-610b4e503ccc5cb6d4ef98bb5016ba42eaf94522.tar.gz rust-610b4e503ccc5cb6d4ef98bb5016ba42eaf94522.zip | |
Rollup merge of #90035 - SparrowLii:rfc2528, r=jackh726
implement rfc-2528 type_changing-struct-update This PR implement rfc2528-type_changing-struct-update. The main change process is as follows: 1. Move the processing part of `base_expr` into `check_expr_struct_fields` to avoid returning `remaining_fields` (a relatively complex hash table) 2. Before performing the type consistency check(`check_expr_has_type_or_error`), if the `type_changing_struct_update` feature is set, enter a different processing flow, otherwise keep the original flow 3. In the case of the same structure definition, check each field in `remaining_fields`. If the field in `base_expr` is not the suptype of the field in `adt_ty`, an error(`FeildMisMatch`) will be reported. The MIR part does not need to be changed, because only the items contained in `remaining_fields` will be extracted from `base_expr` when MIR is generated. This means that fields with different types in `base_expr` will not be used Updates #86618 cc `@nikomatsakis`
Diffstat (limited to 'src/doc')
| -rw-r--r-- | src/doc/unstable-book/src/language-features/type-changing-struct-update.md | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/doc/unstable-book/src/language-features/type-changing-struct-update.md b/src/doc/unstable-book/src/language-features/type-changing-struct-update.md new file mode 100644 index 00000000000..9909cf35b5b --- /dev/null +++ b/src/doc/unstable-book/src/language-features/type-changing-struct-update.md @@ -0,0 +1,33 @@ +# `type_changing_struct_update` + +The tracking issue for this feature is: [#86555] + +[#86555]: https://github.com/rust-lang/rust/issues/86555 + +------------------------ + +This implements [RFC2528]. When turned on, you can create instances of the same struct +that have different generic type or lifetime parameters. + +[RFC2528]: https://github.com/rust-lang/rfcs/blob/master/text/2528-type-changing-struct-update-syntax.md + +```rust +#![allow(unused_variables, dead_code)] +#![feature(type_changing_struct_update)] + +fn main () { + struct Foo<T, U> { + field1: T, + field2: U, + } + + let base: Foo<String, i32> = Foo { + field1: String::from("hello"), + field2: 1234, + }; + let updated: Foo<f64, i32> = Foo { + field1: 3.14, + ..base + }; +} +``` |
