]> git.lizzy.rs Git - rust.git/blob - src/doc/unstable-book/src/language-features/trait-alias.md
Rollup merge of #106904 - khuey:preserve_debuginfo_for_rlibs, r=davidtwco
[rust.git] / src / doc / unstable-book / src / language-features / trait-alias.md
1 # `trait_alias`
2
3 The tracking issue for this feature is: [#41517]
4
5 [#41517]: https://github.com/rust-lang/rust/issues/41517
6
7 ------------------------
8
9 The `trait_alias` feature adds support for trait aliases. These allow aliases
10 to be created for one or more traits (currently just a single regular trait plus
11 any number of auto-traits), and used wherever traits would normally be used as
12 either bounds or trait objects.
13
14 ```rust
15 #![feature(trait_alias)]
16
17 trait Foo = std::fmt::Debug + Send;
18 trait Bar = Foo + Sync;
19
20 // Use trait alias as bound on type parameter.
21 fn foo<T: Foo>(v: &T) {
22     println!("{:?}", v);
23 }
24
25 pub fn main() {
26     foo(&1);
27
28     // Use trait alias for trait objects.
29     let a: &Bar = &123;
30     println!("{:?}", a);
31     let b = Box::new(456) as Box<dyn Foo>;
32     println!("{:?}", b);
33 }
34 ```