简体   繁体   中英

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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