From dc656840efe5197158072ca6eaf8ac14d767995b Mon Sep 17 00:00:00 2001 From: Jeong Yunwon Date: Tue, 12 Mar 2019 14:25:36 +0900 Subject: [PATCH] Add score/lifetimes/trait.md --- src/SUMMARY.md | 1 + src/scope/lifetime/trait.md | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 src/scope/lifetime/trait.md diff --git a/src/SUMMARY.md b/src/SUMMARY.md index d7169f805e..6eca451bcc 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -124,6 +124,7 @@ - [Functions](scope/lifetime/fn.md) - [Methods](scope/lifetime/methods.md) - [Structs](scope/lifetime/struct.md) + - [Traits](scope/lifetime/trait.md) - [Bounds](scope/lifetime/lifetime_bounds.md) - [Coercion](scope/lifetime/lifetime_coercion.md) - [Static](scope/lifetime/static_lifetime.md) diff --git a/src/scope/lifetime/trait.md b/src/scope/lifetime/trait.md new file mode 100644 index 0000000000..c0808ae09a --- /dev/null +++ b/src/scope/lifetime/trait.md @@ -0,0 +1,33 @@ +# Traits + +Annotation of lifetimes in trait methods basically are similar to functions. +Note that `impl` may have annotation of lifetimes too. + +```rust,editable +// A struct with annotation of lifetimes. +#[derive(Debug)] + struct Borrowed<'a> { + x: &'a i32, + } + +// Annotate lifetimes to impl. +impl<'a> Default for Borrowed<'a> { + fn default() -> Self { + Self { + x: &10, + } + } +} + +fn main() { + let b: Borrowed = Default::default(); + println!("b is {:?}", b); +} +``` + +### See also: + +[`trait`s][trait] + + +[trait]: trait.html