std-method

Read::read_to_string: UTF-8 input

Append valid UTF-8 from a reader and report the count in bytes, not characters. Invalid UTF-8 is an error. For arbitrary binary data use a byte vector instead. Expected stdout (not executed): 8 4 Common mistake: Assuming every source contains UTF-8.

Formal Syntax & Structure

rust syntaxrust
reader.read_to_string(&mut String) -> io::Result<usize>

Working Example

Example Snippetrust
fn main() {    use std::io::Read;    let mut src = "سلام".as_bytes();    let mut text = String::new();    let n = src.read_to_string(&mut text).unwrap();    println!("{n} {}", text.chars().count());}
Rust Reference