1 // Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 #![feature(tuple_struct_self_ctor)]
15 use std::fmt::Display;
25 Self(1,2) // Self as a constructor
30 Self(x, y) => println!("{} {}", x, y), // Self as a pattern
35 struct ST2<T>(T); // With type parameter
37 impl<T> ST2<T> where T: Display {
39 fn ctor(v: T) -> Self {
45 Self(ref v) => println!("{}", v),
50 struct ST3<'a>(&'a i32); // With lifetime parameter
54 fn ctor(v: &'a i32) -> Self {
59 let Self(ref v) = self;
67 fn map(opt: Option<usize>) -> Option<Self> {
68 opt.map(Self) // use `Self` as a function passed somewhere
72 struct ST5; // unit struct
76 Self // `Self` as a unit struct value
79 fn pattern(self) -> Self {
81 Self => Self, // `Self` as a unit struct value for matching
90 let v2 = ST2::ctor(10);
94 let v3 = ST3::ctor(&local);
97 let v4 = Some(1usize);
100 let v5 = ST5::ctor();