【发布时间】:2015-01-15 20:47:09
【问题描述】:
我知道我可以读取一行并将其转换为一行中的数字,即
let lines: u32 = io::stdin().read_line().ok().unwrap().trim().parse().unwrap();
如何在没有解析和一行的情况下做同样的事情?现在我这样做:
let line_u = io::stdin().read_line().ok().unwrap();
let line_t = line_u.as_slice().trim();
编辑:解释这里发生了什么:
pub fn stdin() -> StdinReader
fn read_line(&mut self) -> IoResult<String> // method of StdinReader
type IoResult<T> = Result<T, IoError>;
fn ok(self) -> Option<T> // method of Result
fn unwrap(self) -> T // method of Option
fn trim(&self) -> &str // method of str from trait StrExt
fn to_string(?) -> String // I don't know where is this located in documentation
我们可以在 String 上使用 trim,因为 String 是一个用指针修饰的 str,一个拥有的字符串。
parse(), stdin(), read_line(), IoResult, ok(), unwrap(), trim(), str
【问题讨论】:
标签: rust