Der Versuch, &[u8]
als Argument für eine Funktion, die Read
erfordert scheint scheint nicht zu funktionieren, wie ich erwartet hatte, wie im folgenden Beispiel veranschaulicht.Casting & [u8] to std :: io :: Lesen verursacht Größe Problem
use std::io::Read;
fn main() {
let bytes: &[u8] = &[1, 2, 3, 4];
print_reader(&bytes);
}
fn print_reader(reader: &(Read + Sized)) {
for byte in reader.bytes() {
println!("{}", byte.unwrap());
}
}
Compiler-Fehler:
error: the trait bound `std::io::Read + Sized: std::marker::Sized` is not satisfied [--explain E0277]
--> <anon>:9:24
9 |> for byte in reader.bytes() {
|> ^^^^^
note: `std::io::Read + Sized` does not have a constant size known at compile-time
error: the trait bound `std::io::Read + Sized: std::marker::Sized` is not satisfied [--explain E0277]
--> <anon>:9:5
9 |> for byte in reader.bytes() {
|> ^
note: `std::io::Read + Sized` does not have a constant size known at compile-time
note: required because of the requirements on the impl of `std::iter::Iterator` for `std::io::Bytes<std::io::Read + Sized>`
error: aborting due to 2 previous errors
Die Implementierung folgende Eigenschaft kann in der std::slice
Dokumentation:
impl<'a> Read for &'a [u8]
.