std-function

io::repeat: repeating byte source

Generate an unending series of a chosen byte. Combine with take before collecting so the example has a finite memory and time requirement. Expected stdout (not executed): xxxx Common mistake: Calling read_to_end on repeat without a bound.

Formal Syntax & Structure

rust syntaxrust
io::repeat(byte: u8) -> Repeat

Working Example

Example Snippetrust
fn main() {    use std::io::{self, Read};    let mut out = Vec::new();    io::repeat(b'x').take(4).read_to_end(&mut out).unwrap();    println!("{}", String::from_utf8(out).unwrap());}
Rust Reference