mirror of
https://github.com/sunface/rust-by-practice.git
synced 2025-06-23 04:29:41 +00:00
format code in md
This commit is contained in:
@ -1,4 +1,5 @@
|
||||
1、
|
||||
|
||||
```rust
|
||||
struct DoubleRef<'a,'b:'a, T> {
|
||||
r: &'a T,
|
||||
@ -9,8 +10,8 @@ fn main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
2、
|
||||
|
||||
```rust
|
||||
struct ImportantExcerpt<'a> {
|
||||
part: &'a str,
|
||||
@ -29,6 +30,7 @@ fn main() {
|
||||
```
|
||||
|
||||
3、
|
||||
|
||||
```rust
|
||||
fn f<'a, 'b>(x: &'a i32, mut y: &'b i32) where 'a: 'b {
|
||||
y = x; // &'a i32 is a subtype of &'b i32 because 'a: 'b
|
||||
@ -39,8 +41,8 @@ fn main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
4、
|
||||
|
||||
```rust
|
||||
fn call_on_ref_zero<F>(f: F) where for<'a> F: Fn(&'a i32) {
|
||||
let zero = 0;
|
||||
@ -52,7 +54,9 @@ fn main() {
|
||||
}
|
||||
```
|
||||
|
||||
Higher-ranked lifetimes may also be specified just before the trait: the only difference is the scope of the lifetime parameter, which extends only to the end of the following trait instead of the whole bound. This function is equivalent to the last one.
|
||||
Higher-ranked lifetimes may also be specified just before the trait: the only difference is the scope of the lifetime
|
||||
parameter, which extends only to the end of the following trait instead of the whole bound. This function is equivalent
|
||||
to the last one.
|
||||
|
||||
```rust
|
||||
fn call_on_ref_zero<F>(f: F) where F: for<'a> Fn(&'a i32) {
|
||||
@ -62,6 +66,7 @@ fn call_on_ref_zero<F>(f: F) where F: for<'a> Fn(&'a i32) {
|
||||
```
|
||||
|
||||
5、
|
||||
|
||||
```rust
|
||||
fn main() {
|
||||
let mut data = 10;
|
||||
@ -75,8 +80,8 @@ fn main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
6、
|
||||
|
||||
```rust
|
||||
struct Interface<'b, 'a: 'b> {
|
||||
manager: &'b mut Manager<'a>
|
||||
|
@ -1,5 +1,7 @@
|
||||
# Lifetime
|
||||
|
||||
1.
|
||||
|
||||
```rust
|
||||
fn main() {
|
||||
let i = 3; // Lifetime for `i` starts. ────────────────┐
|
||||
@ -20,8 +22,8 @@ fn main() {
|
||||
} // Lifetime ends. ─────────────────────────────────────┘
|
||||
```
|
||||
|
||||
|
||||
2. We can't borrow a item whose lifetime is smaller.
|
||||
|
||||
```rust
|
||||
fn main() {
|
||||
{
|
||||
@ -38,6 +40,7 @@ fn main() {
|
||||
```
|
||||
|
||||
3
|
||||
|
||||
```rust
|
||||
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
|
||||
if x.len() > y.len() {
|
||||
@ -51,6 +54,7 @@ fn main() {}
|
||||
```
|
||||
|
||||
4.
|
||||
|
||||
```rust
|
||||
fn invalid_output() -> String {
|
||||
String::from("foo")
|
||||
@ -76,6 +80,7 @@ fn main() {}
|
||||
```
|
||||
|
||||
5.
|
||||
|
||||
```rust
|
||||
fn print_refs<'a, 'b>(x: &'a i32, y: &'b i32) {
|
||||
println!("x is {} and y is {}", x, y);
|
||||
@ -98,6 +103,7 @@ fn main() {
|
||||
```
|
||||
|
||||
6.
|
||||
|
||||
```rust
|
||||
// A type `Borrowed` which houses a reference to an
|
||||
// `i32`. The reference to `i32` must outlive `Borrowed`.
|
||||
@ -135,6 +141,7 @@ fn main() {
|
||||
```
|
||||
|
||||
7. 🌟
|
||||
|
||||
```rust,editable
|
||||
/* Make it work */
|
||||
|
||||
@ -165,8 +172,8 @@ fn main()
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
8. 🌟
|
||||
|
||||
```rust,editable
|
||||
|
||||
#[derive(Debug)]
|
||||
@ -193,6 +200,7 @@ fn main()
|
||||
```
|
||||
|
||||
9.
|
||||
|
||||
```rust
|
||||
struct ImportantExcerpt<'a> {
|
||||
part: &'a str,
|
||||
@ -207,8 +215,8 @@ impl<'a> ImportantExcerpt<'a> {
|
||||
fn main() {}
|
||||
```
|
||||
|
||||
|
||||
10.
|
||||
|
||||
```rust
|
||||
|
||||
fn nput(x: &i32) {
|
||||
|
@ -1,4 +1,5 @@
|
||||
1、
|
||||
|
||||
```rust
|
||||
fn main() {
|
||||
let v: &str = "hello";
|
||||
@ -26,6 +27,7 @@ fn need_static(r : &'static str) {
|
||||
```
|
||||
|
||||
2、
|
||||
|
||||
```rust
|
||||
#[derive(Debug)]
|
||||
struct Config {
|
||||
@ -54,6 +56,7 @@ fn main() {
|
||||
```
|
||||
|
||||
3、
|
||||
|
||||
```rust
|
||||
fn main() {
|
||||
// Make a `string` literal and print it:
|
||||
@ -65,6 +68,7 @@ fn main() {
|
||||
```
|
||||
|
||||
5、
|
||||
|
||||
```rust
|
||||
use std::fmt::Debug;
|
||||
|
||||
|
Reference in New Issue
Block a user