Skip to content

Commit 9c90223

Browse files
authored
Merge pull request #1168 from youknowone/lifetime-traits
Add score/lifetimes/trait.md
2 parents 746e0c6 + dc65684 commit 9c90223

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@
124124
- [Functions](scope/lifetime/fn.md)
125125
- [Methods](scope/lifetime/methods.md)
126126
- [Structs](scope/lifetime/struct.md)
127+
- [Traits](scope/lifetime/trait.md)
127128
- [Bounds](scope/lifetime/lifetime_bounds.md)
128129
- [Coercion](scope/lifetime/lifetime_coercion.md)
129130
- [Static](scope/lifetime/static_lifetime.md)

src/scope/lifetime/trait.md

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Traits
2+
3+
Annotation of lifetimes in trait methods basically are similar to functions.
4+
Note that `impl` may have annotation of lifetimes too.
5+
6+
```rust,editable
7+
// A struct with annotation of lifetimes.
8+
#[derive(Debug)]
9+
struct Borrowed<'a> {
10+
x: &'a i32,
11+
}
12+
13+
// Annotate lifetimes to impl.
14+
impl<'a> Default for Borrowed<'a> {
15+
fn default() -> Self {
16+
Self {
17+
x: &10,
18+
}
19+
}
20+
}
21+
22+
fn main() {
23+
let b: Borrowed = Default::default();
24+
println!("b is {:?}", b);
25+
}
26+
```
27+
28+
### See also:
29+
30+
[`trait`s][trait]
31+
32+
33+
[trait]: trait.html

0 commit comments

Comments
 (0)