gpt4 book ai didi

rust - Read::read_exact 不填充缓冲区

转载 作者:行者123 更新时间:2023-12-05 09:04:08 26 4
gpt4 key购买 nike

<分区>

我有以下代码:

use std::io::{Cursor, BufReader, Read};

fn main() {
let string = String::from("Cello");
let bytes = string.as_bytes();
let cursor = Cursor::new(bytes);
let mut reader = BufReader::new(cursor);
let mut buf: Vec<u8> = Vec::with_capacity(4);
assert_eq!(4, buf.capacity());
reader.read_exact(&mut buf);
assert_eq!(4, buf.len()); //thread 'main' panicked at 'assertion failed: `(left == right)`
//left: `4`, right: `0`'
}

根据 std::io::read_exact 的文档,它将“读取填充 buf 所需的确切字节数”。但在这种情况下,尽管有 4 个字节的容量,但没有字节被读入 vec。怎么回事?

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com