Rustの標準ライブラリが難しい
標準入力から文字列を読み取るサンプル例
use std::io; fn main() -> io::Result<()> { let mut buffer = String::new(); let stdin = io::stdin(); let mut handle = stdin.lock(); handle.read_to_string(&mut buffer)?; Ok(()) }
上記はコンパイルエラーになります。
10 | handle.read_to_string(&mut buffer)?; | ^^^^^^^^^^^^^^ method not found in `std::io::StdinLock<'_>` |
read_to_string()
メソッドがstd::io::StdinLock<'_>
に存在しない。
これはread_to_string()
メソッドがRead
トレイト側で実装されているためです。
以下エラーの続き、もしかしたらこれで解決するかもとご丁寧に履いてくれています。
= help: items from traits can only be used if the trait is in scope help: the following trait is implemented but not in scope; perhaps add a `use` for it: | 2 | use std::io::Read; |
むずかしい。。。 どこかしらHaskellの型クラス側で実装されているのと似ている気がします。
この辺HaskellerさんがRustの解説本を書いてくれたらと思いますね。