mirror of
https://github.com/sunface/rust-by-practice.git
synced 2025-06-23 04:29:41 +00:00
update lifetime/basic.md
This commit is contained in:
@ -192,6 +192,54 @@ fn main()
|
||||
}
|
||||
```
|
||||
|
||||
9.
|
||||
```rust
|
||||
struct ImportantExcerpt<'a> {
|
||||
part: &'a str,
|
||||
}
|
||||
|
||||
impl<'a> ImportantExcerpt<'a> {
|
||||
fn level(&'a self) -> i32 {
|
||||
3
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
```
|
||||
|
||||
|
||||
10.
|
||||
```rust
|
||||
|
||||
fn nput(x: &i32) {
|
||||
println!("`annotated_input`: {}", x);
|
||||
}
|
||||
|
||||
fn pass(x: &i32) -> &i32 { x }
|
||||
|
||||
fn longest<'a, 'b>(x: &'a str, y: &'b str) -> &'a str {
|
||||
x
|
||||
}
|
||||
|
||||
struct Owner(i32);
|
||||
|
||||
impl Owner {
|
||||
// Annotate lifetimes as in a standalone function.
|
||||
fn add_one(&mut self) { self.0 += 1; }
|
||||
fn print(&self) {
|
||||
println!("`print`: {}", self.0);
|
||||
}
|
||||
}
|
||||
|
||||
struct Person<'a> {
|
||||
age: u8,
|
||||
name: &'a str,
|
||||
}
|
||||
|
||||
enum Either<'a> {
|
||||
Num(i32),
|
||||
Ref(&'a i32),
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
```
|
Reference in New Issue
Block a user