lifetime/static #254
Replies: 15 comments 9 replies
-
第六题直接麻了😵 |
Beta Was this translation helpful? Give feedback.
-
第六题应该怎么理解? |
Beta Was this translation helpful? Give feedback.
-
第6题修改后能编译通过,但不知道对不对。 use std::fmt::Display; fn main() { //string.push_str(string.to_uppercase().as_str()); //println!("{}", string); const string: &str = "FirstFIRST"; print_a(&string); } fn print_a<T: Display + 'static>(t: &T) { fn print_b(t: &T) fn print_c(t: &'static dyn Display) { fn print_d(t: &'static impl Display) { fn print_e(t: &(dyn Display + 'static)) { fn print_f(t: &(impl Display + 'static)) { fn print_g(t: &'static str) { |
Beta Was this translation helpful? Give feedback.
-
抓着一个用:) 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(Box::leak(Box::new(string))); // Compilation error
print_d(Box::leak(Box::new(string))); // Compilation error
print_e(Box::leak(Box::new(string)));
print_f(Box::leak(Box::new(string)));
print_g(Box::leak(Box::new(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);
} |
Beta Was this translation helpful? Give feedback.
-
第二题 #[derive(Debug)] /* 让代码工作,但不要修改函数的签名 */ fn main() {
} |
Beta Was this translation helpful? Give feedback.
-
第六题,可以通过编译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(Box::leak(Box::new(string.clone()))); // Compilation error
print_d(Box::leak(Box::new(string.clone()))); // Compilation error
print_e(&string);
print_f(&string);
print_g(Box::leak(Box::new(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);
} |
Beta Was this translation helpful? Give feedback.
-
第一题:方法1: /* 使用两种方法填空 */
fn main() {
let v = "hello";
need_static(v);
println!("Success!")
}
fn need_static(r : &'static str) {
assert_eq!(r, "hello");
} 方法2: /* 使用两种方法填空 */
fn main() {
static v:&str = "hello";
need_static(v);
println!("Success!")
}
fn need_static(r : &'static str) {
assert_eq!(r, "hello");
} 第二题:#[derive(Debug)]
struct Config {
a: String,
b: String,
}
static mut config: Option<&mut Config> = None;
/* 让代码工作,但不要修改函数的签名 */
fn init() -> Option<&'static mut Config> {
Some(Box::leak(Box::new(Config {
a: "A".to_string(),
b: "B".to_string(),
})))
}
fn main() {
unsafe {
config = init();
println!("{:?}",config)
}
} 第三题:fn main() {
// 字符串字面量能跟程序活得一样久,因此 `static_string` 的生命周期是 `'static`
let static_string = "I'm in read-only memory";
println!("static_string: {}", static_string);
// 当 `static_string` 超出作用域时,该引用就无法再被使用,但是引用指向的数据( 字符串字面量 ) 依然保存在二进制 binary 所占用的内存中
println!("static_string reference remains alive: {}", static_string);
} 第四题:无 第五题:/* 让代码工作 */
use std::fmt::Debug;
fn print_it<T: Debug + 'static>( input: T) {
println!( "'static value passed in is: {:?}", input );
}
fn print_it1<T:Debug + 'static>( input:&T ) {
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 是有所有权的数据,并没有包含任何引用,因此它是 'static
let i = 5;
print_it(i);
// 但是 &i 是一个引用,生命周期受限于作用域,因此它不是 'static
print_it(i);
print_it1(&i);
// 但是下面的代码可以正常运行 !
print_it2(&i);
} 第六题: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(Box::leak(Box::new(string.clone()))); // Compilation error
print_d(Box::leak(Box::new(string.clone()))); // Compilation error
print_e(&string);
print_f(&string);
print_g(Box::leak(Box::new(string.clone()))); // 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);
} |
Beta Was this translation helpful? Give feedback.
-
关于第四题, 这里的描述是不是有点误导, 并不是强制转变了 比如函数可以写成: fn coerce_static<'a>(_: &'a i32) -> &'a i32 {
let x = &NUM;
x
} 而更进一步main也能直接写成: fn main() {
{
// let lifetime_num = 9;
// let coerced_static = coerce_static(&lifetime_num);
let coerced_static = &NUM;
println!("coerced_static: {}", coerced_static);
}
println!("NUM: {} stays accessible!", NUM);
}
如果有错希望大家指正, 提前谢谢! |
Beta Was this translation helpful? Give feedback.
-
第六题use std::fmt::Display;
fn main() {
let mut string = "First".to_owned();
string.push_str(string.to_uppercase().as_str());
// 泄露
let string = Box::leak(Box::new(string));
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);
} |
Beta Was this translation helpful? Give feedback.
-
修改方法,可以通过编译 fn main() {
} fn print_a<T: Display + 'static>(t: &T) { fn print_b(t: &T) fn print_c(t: &dyn Display) { fn print_d(t: &impl Display) { fn print_e(t: &(dyn Display + 'static)) { fn print_f(t: &(impl Display + 'static)) { fn print_g(t: &str) { |
Beta Was this translation helpful? Give feedback.
-
use std::fmt::Display; fn main() { string.push_str(string.to_uppercase().as_str()); print_a(string); fn print_a<T: Display + 'static>(t: &T) { fn print_b(t: &T) fn print_c(t: &'static dyn Display) { fn print_d(t: &'static impl Display) { fn print_e(t: &(dyn Display + 'static)) { fn print_f(t: &(impl Display + 'static)) { fn print_g(t: &'static String) { |
Beta Was this translation helpful? Give feedback.
-
第六通杀: Box::leak(Box::new(string.clone())) |
Beta Was this translation helpful? Give feedback.
-
看到一篇帖子:https://www.jianshu.com/p/500ed3635a41 |
Beta Was this translation helpful? Give feedback.
-
第三题 诶,就是玩fn main() {
let static_string = "I'm in read-only memory";
let ptr_s: *const u8 = static_string.as_ptr();
let len = static_string.len();
unsafe {
// 将裸指针和长度转换为切片
let slice = std::slice::from_raw_parts(ptr_s, len);
// 将切片转换为字符串
println!("static_string: {}", std::str::from_utf8(slice).unwrap())
}
} |
Beta Was this translation helpful? Give feedback.
-
&'static T ,static修饰的是引用,因此无论是&String还是 dyn/impl Display 都无法通过编译。 |
Beta Was this translation helpful? Give feedback.
-
lifetime/static
Learn Rust with Example, Exercise and real Practice, written with ❤️ by https://course.rs team
https://practice.rs/lifetime/static.html
Beta Was this translation helpful? Give feedback.
All reactions