]> git.lizzy.rs Git - rust.git/blob - src/doc/unstable-book/src/language-features/self-struct-ctor.md
Changed test for issue 56202 to compile-pass.
[rust.git] / src / doc / unstable-book / src / language-features / self-struct-ctor.md
1 # `self_struct_ctor`
2
3 The tracking issue for this feature is: [#51994]
4 [#51994]: https://github.com/rust-lang/rust/issues/51994
5
6 ------------------------
7
8 The `self_struct_ctor` feature gate lets you use the special `Self`
9 identifier as a constructor and a pattern.
10
11 A simple example is:
12
13 ```rust
14 #![feature(self_struct_ctor)]
15
16 struct ST(i32, i32);
17
18 impl ST {
19     fn new() -> Self {
20         ST(0, 1)
21     }
22
23     fn ctor() -> Self {
24         Self(1,2)           // constructed by `Self`, it is the same as `ST(1, 2)`
25     }
26
27     fn pattern(self) {
28         match self {
29             Self(x, y) => println!("{} {}", x, y), // used as a pattern
30         }
31     }
32 }
33 ```