Skip to content

Commit 40a4c7c

Browse files
committed
lifetimes
1 parent a022210 commit 40a4c7c

4 files changed

Lines changed: 8 additions & 37 deletions

File tree

content/en/docs/c3.lifetimes.md

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ fn take_str(x: &'static str) {
5353

5454
## Usage
5555

56-
Lifetimes are denoted with an apostrophe. By convention, a lowercase letter is used for naming. Usually **starts with** `'a` and **follows alphabetic order** when we need to add **multiple lifetime** annotations.
57-
5856
When using references,
5957

6058
### 01. On Function Declaration
@@ -161,15 +159,11 @@ impl<F> Struct<F> where for<'a> F: FnOnce(&'a Type) { fn x(&self) -> &F { &self.
161159

162160
## Lifetime Elision
163161

164-
As I mentioned earlier, in order to make **common patterns** more ergonomic, Rust allows lifetimes to be **elided/omitted**. This process is called **Lifetime Elision**.
165-
166-
💡 For the moment Rust supports Lifetime Elisions only on `fn` definitions. But in the future, it will support for `impl` headers as well.
167-
168162
Lifetime annotations of `fn` definitions can be elided
169163
if its **parameter list** has either,
170164

171165
* **only one input parameter passes by reference**.
172-
* a parameter with **either** `&self` **or** **&mut self** reference.
166+
* a parameter with **either `&self` or `&mut self` reference**.
173167

174168
```rust
175169
fn triple(x: &u64) -> u64 { // Only one input parameter passes by reference
@@ -220,22 +214,6 @@ fn main() {
220214
> * For all other cases, we have to write lifetime annotations manually.
221215
222216

223-
## `'static` Annotations
224-
225-
`'static` lifetime annotation is a **reserved** lifetime annotation. These **references are valid for the entire program**. They are saved in the data segment of the binary and the data referred to will never go out of scope.
226-
227-
```rust
228-
static N: i32 = 5; // A constant with 'static lifetime
229-
230-
let a = "Hello, world."; // a: &'static str
231-
232-
233-
fn index() -> &'static str { // No need to mention <'static> ; fn index ̶<̶'̶s̶t̶a̶t̶i̶c̶>̶
234-
"Hello, world!"
235-
}
236-
```
237-
238-
239217
## Few more examples about the usage of Rust lifetimes.
240218

241219
```rust

0 commit comments

Comments
 (0)