rust-by-practice/solutions/lifetime/static.md

103 lines
1.6 KiB
Markdown
Raw Permalink Normal View History

2022-11-06 22:06:19 -06:00
1.
2022-05-01 00:08:50 -05:00
2022-03-29 07:55:18 -06:00
```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");
}
```
2022-11-06 22:06:19 -06:00
2.
2022-05-01 00:08:50 -05:00
2022-03-29 07:55:18 -06:00
```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)
}
}
```
2022-11-06 22:06:19 -06:00
3.
2022-05-01 00:08:50 -05:00
2022-03-29 07:55:18 -06:00
```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);
}
```
2022-11-06 22:06:19 -06:00
5.
2022-05-01 00:08:50 -05:00
2022-03-29 07:55:18 -06:00
```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);
}
```