简体   繁体   English

Rust - 与 Arc 一起使用时无法读取互斥锁内的文件

[英]Rust - Unable to read a file inside a mutex when it is used with an Arc

I am unable to get this code to work, it always return an empty string and no bytes are read我无法让这段代码工作,它总是返回一个空字符串,并且没有读取任何字节

let mut contents = String::new();
let file_ref = Arc::new(Mutex::new(my_file));
let mut guard = file_ref.lock().unwrap();
let bytes = guard
    .read_to_string(&mut contents)
    .expect("File to be read");
dbg!(bytes);

It seems a file can be read only once, I was reading the my_file in previous lines, looks like that seems to be the issue and I need to clone the file pointer itself too...似乎一个文件只能读取一次,我在前几行中读取了my_file ,看起来这似乎是问题所在,我也需要克隆文件指针本身......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM