Rust 惰性\u静态全局字符串将不打印
我已经写了这个代码,我一直有问题。。。字符串将不会打印,我不知道我在这里做错了什么Rust 惰性\u静态全局字符串将不打印,rust,atom-editor,Rust,Atom Editor,我已经写了这个代码,我一直有问题。。。字符串将不会打印,我不知道我在这里做错了什么 使用lazy\u static::lazy\u static; 使用std::io::{self,Write}; 使用std::sync::Mutex; 懒惰的人!{ 静态引用示例:Mutex=Mutex::new(“示例字符串”.to_字符串()); } fn main(){ println!(“{}”,示例); } 我不知道哪里出了问题,我对这整件生锈的事情还不熟悉,但我知道我需要在这里使用一个全局可变变量
使用lazy\u static::lazy\u static;
使用std::io::{self,Write};
使用std::sync::Mutex;
懒惰的人!{
静态引用示例:Mutex=Mutex::new(“示例字符串”.to_字符串());
}
fn main(){
println!(“{}”,示例);
}
我不知道哪里出了问题,我对这整件生锈的事情还不熟悉,但我知道我需要在这里使用一个全局可变变量和lazy_static,所以我需要它来工作…
示例
不是字符串。它是一个互斥体
,包含字符串
。在访问互斥对象的内容之前,必须锁定
互斥对象:
use lazy_static::lazy_static;
use std::io::{self, Write};
use std::sync::Mutex;
lazy_static! {
static ref example: Mutex<String> = Mutex::new("example string".to_string());
}
fn main(){
println!("{}", example.lock().expect("Could not lock mutes"));
}
使用lazy\u static::lazy\u static;
使用std::io::{self,Write};
使用std::sync::Mutex;
懒惰的人!{
静态引用示例:Mutex=Mutex::new(“示例字符串”.to_字符串());
}
fn main(){
println!(“{}”,example.lock().expect(“无法锁定静音”);
}
资料来源:引述:
每个互斥锁都有一个类型参数,该参数表示互斥锁所在的数据
保护。数据只能通过RAII防护装置访问
从lock
和try\u lock
返回,它保证数据
只有当互斥锁被锁定时才能访问
非常感谢你,我曾试图锁上它,但运气不好,我记得我忘记了.expect()部分