mirror of
https://github.com/sunface/rust-by-practice.git
synced 2025-06-26 05:59:42 +00:00
update repo layout
This commit is contained in:
1
en/src/lifetime/advance.md
Normal file
1
en/src/lifetime/advance.md
Normal file
@ -0,0 +1 @@
|
||||
# advance
|
62
en/src/lifetime/basic.md
Normal file
62
en/src/lifetime/basic.md
Normal file
@ -0,0 +1,62 @@
|
||||
## Lifetime
|
||||
1. 🌟
|
||||
```rust,editable
|
||||
/* Make it work */
|
||||
|
||||
#[derive(Debug)]
|
||||
struct NoCopyType {}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Example<'a, 'b> {
|
||||
a: &'a u32,
|
||||
b: &'b NoCopyType
|
||||
}
|
||||
|
||||
fn main()
|
||||
{
|
||||
/* 'a tied to fn-main stackframe */
|
||||
let var_a = 35;
|
||||
let example: Example;
|
||||
|
||||
{
|
||||
/* lifetime 'b tied to new stackframe/scope */
|
||||
let var_b = NoCopyType {};
|
||||
|
||||
/* fixme */
|
||||
example = Example { a: &var_a, b: &var_b };
|
||||
}
|
||||
|
||||
println!("(Success!) {:?}", example);
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
2. 🌟
|
||||
```rust,editable
|
||||
|
||||
#[derive(Debug)]
|
||||
struct NoCopyType {}
|
||||
|
||||
#[derive(Debug)]
|
||||
#[allow(dead_code)]
|
||||
struct Example<'a, 'b> {
|
||||
a: &'a u32,
|
||||
b: &'b NoCopyType
|
||||
}
|
||||
|
||||
/* Fix function signature */
|
||||
fn fix_me(foo: &Example) -> &NoCopyType
|
||||
{ foo.b }
|
||||
|
||||
fn main()
|
||||
{
|
||||
let no_copy = NoCopyType {};
|
||||
let example = Example { a: &1, b: &no_copy };
|
||||
fix_me(&example);
|
||||
print!("Success!")
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
5
en/src/lifetime/intro.md
Normal file
5
en/src/lifetime/intro.md
Normal file
@ -0,0 +1,5 @@
|
||||
# Lifetime
|
||||
Learning resources:
|
||||
- English: [Rust Book 10.3](https://doc.rust-lang.org/book/ch07-00-managing-growing-projects-with-packages-crates-and-modules.html)
|
||||
- 简体中文: [Rust语言圣经 - 生命周期](https://course.rs/advance/lifetime/intro.html)
|
||||
|
49
en/src/lifetime/static.md
Normal file
49
en/src/lifetime/static.md
Normal file
@ -0,0 +1,49 @@
|
||||
# &'static and T: 'static
|
||||
|
||||
```rust,editable
|
||||
use std::fmt::Display;
|
||||
|
||||
fn main() {
|
||||
let mut string = "First".to_owned();
|
||||
|
||||
string.push_str(string.to_uppercase().as_str());
|
||||
print_a(&string);
|
||||
print_b(&string);
|
||||
print_c(&string); // Compilation error
|
||||
print_d(&string); // Compilation error
|
||||
print_e(&string);
|
||||
print_f(&string);
|
||||
print_g(&string); // Compilation error
|
||||
}
|
||||
|
||||
fn print_a<T: Display + 'static>(t: &T) {
|
||||
println!("{}", t);
|
||||
}
|
||||
|
||||
fn print_b<T>(t: &T)
|
||||
where
|
||||
T: Display + 'static,
|
||||
{
|
||||
println!("{}", t);
|
||||
}
|
||||
|
||||
fn print_c(t: &'static dyn Display) {
|
||||
println!("{}", t)
|
||||
}
|
||||
|
||||
fn print_d(t: &'static impl Display) {
|
||||
println!("{}", t)
|
||||
}
|
||||
|
||||
fn print_e(t: &(dyn Display + 'static)) {
|
||||
println!("{}", t)
|
||||
}
|
||||
|
||||
fn print_f(t: &(impl Display + 'static)) {
|
||||
println!("{}", t)
|
||||
}
|
||||
|
||||
fn print_g(t: &'static String) {
|
||||
println!("{}", t);
|
||||
}
|
||||
```
|
Reference in New Issue
Block a user