2017-03-01から1ヶ月間の記事一覧
https://doc.rust-lang.org/book/release-channels.html Rustは Nightly Beta Stable の3種類がある。 Betaは6週間間隔でバージョンが上がるプロセスが出来上がっているみたい。リリースマネジメント(?)が着実に行われている。
https://doc.rust-lang.org/book/borrow-and-asref.html BorrowとAsRefについて。 サンプルコードを動かしてみた。 use std::borrow::Borrow; use std::fmt::Display; fn foo<T: Borrow<i32> + Display>(a: T) { println!("a is borrowed: {}", a); } fn bar<T: AsRef<str>>(s: T) { let m</t:></t:>…
https://doc.rust-lang.org/book/ffi.html 他言語で記載されたライブラリをRustから使うための方法。ライブラリのパスをさくっと通せなかったので、動かしてみるのをあきらめた。
https://doc.rust-lang.org/book/choosing-your-guarantees.html Guaranteesは保証という意味の単語らしい。 use std::cell::Cell; use std::cell::RefCell; fn main() { let x = Cell::new(1); let y = &x; let z = &x; x.set(2); y.set(3); z.set(4); prin…
https://doc.rust-lang.org/book/error-handling.html サンプルの'‘city-pop’‘を最後まで。 extern crate getopts; extern crate rustc_serialize; extern crate csv; use getopts::Options; use std::env; use std::fs::File; use std::path::Path; use std…
https://doc.rust-lang.org/book/error-handling.html 今日は、Error handling with a custom typeの手前まで。
https://doc.rust-lang.org/book/error-handling.html Reading from stdinの手前まででビルド通った。 extern crate getopts; extern crate rustc_serialize; extern crate csv; use getopts::Options; use std::env; use std::fs::File; use std::path::Pat…
コンビニにおいてあるのをみかけて、つい立ち読みしちゃいました。 セリフがないからこそ感じられる躍動感があります。 電子書籍化するあたりでちゃんと購入しよう。 BLUE GIANT 10 (ビッグコミックススペシャル)作者: 石塚真一出版社/メーカー: 小学館発売…
https://doc.rust-lang.org/book/error-handling.html Errorというtraitが存在する。
https://doc.rust-lang.org/book/error-handling.html エラーの定義。 use std::io; use std::num; // We derive `Debug` because all types should probably derive `Debug`. // This gives us a reasonable human readable description of `CliError` valu…
https://doc.rust-lang.org/book/error-handling.html ファイル処理の異常処理の続き。エラーの時の処理が明示的になってきたように思う。 use std::fs::File; use std::io::Read; use std::path::Path; fn file_double<P: AsRef<Path>>(file_path: P) -> Result<i32, String> { let mut </i32,></p:>…
https://doc.rust-lang.org/book/error-handling.html use std::fs::File; use std::io::Read; use std::path::Path; fn file_double<P: AsRef<Path>>(file_path: P) -> i32 { let mut file = File::open(file_path).unwrap(); // error 1 let mut contents = String::new()</p:>…
https://doc.rust-lang.org/book/error-handling.html use std::env;すると、argv.nthで引数をとれる。 引数の有無でエラー判定するための方法。 use std::env; fn ok_or<T, E>(option: Option<T>, err: E) -> Result<T, E> { match option { Some(val) => Ok(val), None =></t,></t></t,>…
https://doc.rust-lang.org/book/error-handling.html 文字列を整数にパースするときのエラーハンドリング。 use std::num::ParseIntError; fn double_number(number_str: &str) -> Result<i32, ParseIntError> { number_str.parse::<i32>().map(|n| 2 * n) } fn main() { match doubl</i32></i32,>…
https://doc.rust-lang.org/book/error-handling.html とても長い。基本はpanic!マクロを使ってエラーを発生させるということらしい。
https://doc.rust-lang.org/book/concurrency.html 並行処理続き。channelについて。rxからtxにデータが渡るまで、tx側は処理を待つ。 use std::thread; use std::sync::mpsc; fn main() { let (tx, rx) = mpsc::channel(); for i in 0..10 { let tx = tx.cl…
https://doc.rust-lang.org/book/concurrency.html 並列処理について。 use std::sync::{Arc, Mutex}; use std::thread; use std::time::Duration; fn main() { let data = Arc::new(Mutex::new(vec![1, 2, 3])); for i in 0..3 { let data = data.clone(); …
https://doc.rust-lang.org/book/iterators.html fn main() { let num = (1..) .filter(|&x| x % 2 == 0) .filter(|&x| x % 3 == 0) .take(5) .collect::<Vec<i32>>(); for n in num.iter() { println!("{}", n); } } $ cargo run Finished debug [unoptimized + debu</vec<i32>…