mirror of
https://github.com/sunface/rust-by-practice.git
synced 2025-06-23 04:29:41 +00:00
Add [&'static and T: 'static] chapter
This commit is contained in:
98
solutions/lifetime/static.md
Normal file
98
solutions/lifetime/static.md
Normal file
@ -0,0 +1,98 @@
|
||||
1、
|
||||
```rust
|
||||
fn main() {
|
||||
let v: &str = "hello";
|
||||
need_static(v);
|
||||
|
||||
println!("Success!")
|
||||
}
|
||||
|
||||
fn need_static(r : &'static str) {
|
||||
assert_eq!(r, "hello");
|
||||
}
|
||||
```
|
||||
|
||||
```rust
|
||||
fn main() {
|
||||
const v: &str = "hello";
|
||||
need_static(v);
|
||||
|
||||
println!("Success!")
|
||||
}
|
||||
|
||||
fn need_static(r : &'static str) {
|
||||
assert_eq!(r, "hello");
|
||||
}
|
||||
```
|
||||
|
||||
2、
|
||||
```rust
|
||||
#[derive(Debug)]
|
||||
struct Config {
|
||||
a: String,
|
||||
b: String,
|
||||
}
|
||||
static mut config: Option<&mut Config> = None;
|
||||
|
||||
fn init() -> Option<&'static mut Config> {
|
||||
let c = Box::new(Config {
|
||||
a: "A".to_string(),
|
||||
b: "B".to_string(),
|
||||
});
|
||||
|
||||
Some(Box::leak(c))
|
||||
}
|
||||
|
||||
|
||||
fn main() {
|
||||
unsafe {
|
||||
config = init();
|
||||
|
||||
println!("{:?}",config)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
3、
|
||||
```rust
|
||||
fn main() {
|
||||
// Make a `string` literal and print it:
|
||||
let static_string = "I'm in read-only memory";
|
||||
println!("static_string: {}", static_string);
|
||||
|
||||
println!("static_string reference remains alive: {}", static_string);
|
||||
}
|
||||
```
|
||||
|
||||
5、
|
||||
```rust
|
||||
use std::fmt::Debug;
|
||||
|
||||
fn print_it<T: Debug + 'static>( input: T) {
|
||||
println!( "'static value passed in is: {:?}", input );
|
||||
}
|
||||
|
||||
fn print_it1( input: impl Debug + 'static ) {
|
||||
println!( "'static value passed in is: {:?}", input );
|
||||
}
|
||||
|
||||
|
||||
fn print_it2<T: Debug + 'static>( input: &T) {
|
||||
println!( "'static value passed in is: {:?}", input );
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// i is owned and contains no references, thus it's 'static:
|
||||
const i:i32 = 5;
|
||||
print_it(i);
|
||||
|
||||
// oops, &i only has the lifetime defined by the scope of
|
||||
// main(), so it's not 'static:
|
||||
print_it(&i);
|
||||
|
||||
print_it1(&i);
|
||||
|
||||
// but this one WORKS !
|
||||
print_it2(&i);
|
||||
}
|
||||
```
|
Reference in New Issue
Block a user