mirror of
https://github.com/sunface/rust-by-practice.git
synced 2025-06-23 20:49:41 +00:00
fix: correct , to .
This commit is contained in:
@ -39,7 +39,7 @@ fn main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
1,🌟
|
1. 🌟
|
||||||
```rust,editable
|
```rust,editable
|
||||||
/* Annotate struct with lifetime:
|
/* Annotate struct with lifetime:
|
||||||
1. `r` and `s` must have different lifetimes
|
1. `r` and `s` must have different lifetimes
|
||||||
@ -55,7 +55,7 @@ fn main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
2,🌟🌟
|
2. 🌟🌟
|
||||||
```rust,editable
|
```rust,editable
|
||||||
/* Adding trait bounds to make it work */
|
/* Adding trait bounds to make it work */
|
||||||
struct ImportantExcerpt<'a> {
|
struct ImportantExcerpt<'a> {
|
||||||
@ -74,7 +74,7 @@ fn main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
3,🌟🌟
|
3. 🌟🌟
|
||||||
```rust,editable
|
```rust,editable
|
||||||
/* Adding trait bounds to make it work */
|
/* Adding trait bounds to make it work */
|
||||||
fn f<'a, 'b>(x: &'a i32, mut y: &'b i32) {
|
fn f<'a, 'b>(x: &'a i32, mut y: &'b i32) {
|
||||||
@ -100,7 +100,7 @@ and could then be used to compare a `&'a T` with any lifetime to an `i32`.
|
|||||||
|
|
||||||
Only a higher-ranked bound can be used here, because the lifetime of the reference is shorter than any possible lifetime parameter on the function.
|
Only a higher-ranked bound can be used here, because the lifetime of the reference is shorter than any possible lifetime parameter on the function.
|
||||||
|
|
||||||
4,🌟🌟🌟
|
4. 🌟🌟🌟
|
||||||
```rust,editable
|
```rust,editable
|
||||||
/* Adding HRTB to make it work!*/
|
/* Adding HRTB to make it work!*/
|
||||||
fn call_on_ref_zero<'a, F>(f: F) where F: Fn(&'a i32) {
|
fn call_on_ref_zero<'a, F>(f: F) where F: Fn(&'a i32) {
|
||||||
@ -187,7 +187,7 @@ fn main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
5,🌟🌟
|
5. 🌟🌟
|
||||||
```rust,editable
|
```rust,editable
|
||||||
/* Make it work by reordering some code */
|
/* Make it work by reordering some code */
|
||||||
fn main() {
|
fn main() {
|
||||||
@ -235,7 +235,7 @@ struct Ref<'a, T> {
|
|||||||
|
|
||||||
## A difficult exercise
|
## A difficult exercise
|
||||||
|
|
||||||
6,🌟🌟🌟🌟
|
6. 🌟🌟🌟🌟
|
||||||
```rust,editable
|
```rust,editable
|
||||||
/* Make it work */
|
/* Make it work */
|
||||||
struct Interface<'a> {
|
struct Interface<'a> {
|
||||||
|
@ -32,7 +32,7 @@ fn need_static(r : &'static str) {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
2, 🌟🌟🌟🌟 Another way to make `'static` lifetime is using `Box::leak`
|
2. 🌟🌟🌟🌟 Another way to make `'static` lifetime is using `Box::leak`
|
||||||
```rust,editable
|
```rust,editable
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Config {
|
struct Config {
|
||||||
@ -59,7 +59,7 @@ fn main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
3, 🌟 `&'static` only indicates that the data can live forever, not the reference. The latter one will be constrained by its scope.
|
3. 🌟 `&'static` only indicates that the data can live forever, not the reference. The latter one will be constrained by its scope.
|
||||||
```rust,editable
|
```rust,editable
|
||||||
fn main() {
|
fn main() {
|
||||||
{
|
{
|
||||||
@ -75,7 +75,7 @@ fn main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
4, `&'static` can be coerced to a shorter lifetime.
|
4. `&'static` can be coerced to a shorter lifetime.
|
||||||
|
|
||||||
**Example**
|
**Example**
|
||||||
```rust,editable
|
```rust,editable
|
||||||
@ -112,7 +112,7 @@ As a trait bound, it means the type does not contain any non-static references.
|
|||||||
It's important to understand this means that any owned data always passes a `'static `lifetime bound, but a reference to that owned data generally does not.
|
It's important to understand this means that any owned data always passes a `'static `lifetime bound, but a reference to that owned data generally does not.
|
||||||
|
|
||||||
|
|
||||||
5,🌟🌟
|
5. 🌟🌟
|
||||||
```rust,editable
|
```rust,editable
|
||||||
/* Make it work */
|
/* Make it work */
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
@ -147,7 +147,7 @@ fn main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
6,🌟🌟🌟
|
6. 🌟🌟🌟
|
||||||
```rust,editable
|
```rust,editable
|
||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user