std::io::* 1. Trait std::io::Read

DongSub_Joung·2022년 1월 6일
0

std::io

목록 보기
3/4
post-thumbnail

io 하위의 method들을 정리한 문서

trait.Read

Please note that each call to read() may involve a system call, and therefore, using something that implements BufRead, such as BufReader, will be more efficient.

File

use std::io;
use std::io::prelude::*;
use std::fs::File;

fn main() -> io::Result<()> {
    let mut f = File::open("foo.txt")?;
    let mut buffer = [0; 10];

    // read up to 10 bytes
    f.read(&mut buffer)?;

    let mut buffer = Vec::new();
    // read the whole file
    f.read_to_end(&mut buffer)?;

    // read into a String, so that you don't need to do the conversion.
    let mut buffer = String::new();
    f.read_to_string(&mut buffer)?;

    // and more! See the other methods for more details.
    Ok(())
}

fn read(&mut self, buf: &mut [u8]) -> Result // Required

  • This function does not provide any guarantees about whether it blocks waiting for data, but if an object needs to block for a read and cannot, it will typically signal this via an Err return value.
  • An error of the ErrorKind::Interrupted kind is non-fatal and the read operation should be retried if there is nothing else to do.

pub fn read_to_string<R: Read>(reader: &mut R) -> Result

https://doc.rust-lang.org/std/io/fn.read_to_string.html

(If you use Read::read_to_string you have to remember to check whether the read succeeded because otherwise your buffer will be empty or only partially full.)

  • EOF 반환 하거나 위의 조건을 만족하면 String이 반환된다.(무제한으로 입력된 이유는 이러한 특성때문이다.)
  • Reading 중에 오류 발생시 재사용 불가
  • Safe over Performance
  • Err에 String이 wrapped되어 있는 형태의 Result type 반환.

Provided methods

fn readvectored(&mut self, bufs: &mut [IoSliceMut<"'">] -> Result

fn read_to_end(&mut self, buf: &mut Vec -> Result

fn read_exact(&mut self, buf: &mut [u8] -> Result<()>

fn by_ref(&mut self) -> &mut Self

fn bytes(self) -> Bytes

fn chain<R: Read>(self, next: R) -> Chain<Self, R>

fn take(self, limit: u64) -> Take

Self: Sized

0개의 댓글