Не забудьте сбросить позицию чтения с

аюсь использовать то же самоеstd::fs::File объект для записи и чтения, но чтение возвращает пустую строку.

Я попыталсяflush, sync_all а такжеseek, но ничего не помогло. С новымFile объект я могу легко прочитать файл.

use std::io::{Read, Seek, Write};

const FILE_PATH: &str = "test.txt";

fn main() {
    // Create file
    let mut f = std::fs::File::create(FILE_PATH).unwrap();
    f.write_all("foo bar".as_bytes());
    f.seek(std::io::SeekFrom::Start(0));

    // Read from the same descriptor
    let mut content = String::new();
    f.read_to_string(&mut content);
    println!("{:?}", content); // -> ""

    // Read from the other descriptor
    let mut f = std::fs::File::open(FILE_PATH).unwrap();
    let mut content = String::new();
    f.read_to_string(&mut content);
    println!("{:?}", content); // -> "foo bar"
}

Ответы на вопрос(1)

Ваш ответ на вопрос