Box就是unique_ptr 这个函数的功能是消费box返回一个全局变量! 写一个函数,想要真的返回全局变量,感觉用这个是个好的做法

fn Foo()->Option<&'static mut A>

{

let a = Box::new(A());

Some(Box::leak(a))

}

这样就能当真拿到这个全局变量了

还有一种是lazy macro use lazy_static::lazy_static; 然后包在里面 这是圣经里的例子

static NAMES: Mutex = Mutex::new(String::from("Sunface, Jack, Allen"));

GG error[E0015]: calls in statics are limited to constant functions, tuple structs and tuple variants

lazy_static! {

static ref NAMES: Mutex = Mutex::new(String::from("Sunface, Jack, Allen"));

}

OK

相关文章

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: