rust

Release Channels

https://doc.rust-lang.org/book/release-channels.html Rustは Nightly Beta Stable の3種類がある。 Betaは6週間間隔でバージョンが上がるプロセスが出来上がっているみたい。リリースマネジメント(?)が着実に行われている。

Borrow and AsRef

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:>…

FFI

https://doc.rust-lang.org/book/ffi.html 他言語で記載されたライブラリをRustから使うための方法。ライブラリのパスをさくっと通せなかったので、動かしてみるのをあきらめた。

Choosing your Guarantees

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…

Error Handling #10

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…

Error Handling #9

https://doc.rust-lang.org/book/error-handling.html 今日は、Error handling with a custom typeの手前まで。

Error Handling #8

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…

Error Handling #7

https://doc.rust-lang.org/book/error-handling.html Errorというtraitが存在する。

Error Handling #6

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…

Error Handling #5

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:>…

Error Handling #4

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:>…

Error Handling #3

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,>…

Error Handling #2

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,>…

Error Handling

https://doc.rust-lang.org/book/error-handling.html とても長い。基本はpanic!マクロを使ってエラーを発生させるということらしい。

Concurrency #2

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…

Concurrency

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(); …

Iterators

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>…

Documentation

https://doc.rust-lang.org/book/documentation.html ソースコードに特殊なコメントを記載しておくと、ツールでドキュメント生成出来る。Javaのjavadocや、Python等々サポートしている言語は多々ある有りますね。出自はクヌース先生の文芸的プログラミングで…

Conditional Compilation

https://doc.rust-lang.org/book/conditional-compilation.html #[cfg]にて色々設定できる。C言語でいう#if、#ifdefみたいなもののようです。

Testing

https://doc.rust-lang.org/book/testing.html テストの実施環境について。言語に標準でテストの仕組みが含んでいたり、ドキュメントに記載されている。これだけでも今どきの言語だという感じがします。 テストの実施方法はドキュメントを読んだ限りではシン…

The Stack and the Heap

https://doc.rust-lang.org/book/the-stack-and-the-heap.html スタックとヒープについて。 変数宣言でスタックが積み上がっていくなど、目新しいところはなさそう。

Unsafe

[https://doc.rust-lang.org/book/unsafe.html 危ない操作をどうしても行いたい時はunsafeを使う。unsafeを使わざるを得ない事は少ないようだ。

Raw Pointers

https://doc.rust-lang.org/book/raw-pointers.html 単に*で宣言したポインタは安全ではない(何も指し示していないかもしれない)ので、unsafe内で展開して使用する。 fn main() { let x = 5; let raw = &x as *const i32; // println!("raw points at {}", *…

Macros #2

https://doc.rust-lang.org/book/macros.html 再帰的にマクロを使ったり、マクロは奥が深そう。 章の量も他の章と比べ、明らかに多い。 unreachable!が面白い。デバッグ等で活躍しそう。 if false { unreachable!(); }

Macros

https://doc.rust-lang.org/book/macros.html マクロ。 /* vec!と同じ働きをするマクロ定義 */ macro_rules! myvec { ( $( $x:expr ),* ) => { { let mut temp_vec = Vec::new(); $( temp_vec.push($x); )* temp_vec } }; } macro_rules! o_O { ( $( $x:expr…

`Deref` coercions

https://doc.rust-lang.org/book/deref-coercions.html *も型に合わせて定義できる。 use std::ops::Deref; struct DerefExample<T> { value: T, } impl<T> Deref for DerefExample<T> { type Target = T; fn deref(&self) -> &T { &self.value } } fn main() { let x</t></t></t>…

Operators and Overloading

https://doc.rust-lang.org/book/operators-and-overloading.html 新たに定義した型に対して+を定義できる。 use std::ops::Add; #[derive(Debug)] struct Point { x: i32, y: i32, } impl Add for Point { type Output = Point; fn add(self, other: Point)…

Unsized Types

https://doc.rust-lang.org/book/unsized-types.html サイズ不定の型を定義できるみたい。Rustは型の扱いが厳格だと思っていたので驚き。

Associated Types

https://doc.rust-lang.org/book/associated-types.html trait Graph { type N; type E; fn has_edge(&self, &Self::N, &Self::N) -> bool; fn edges(&self, &Self::N) -> Vec<Self::E>; } struct Node; struct Edge; struct MyGraph; impl Graph for MyGraph { type </self::e>…

Casting Between Types

https://doc.rust-lang.org/book/casting-between-types.html 型のキャストについて。asを使用する。 use std::mem; fn main() { let one = true as u8; let at_sign = 64 as char; let two_hundred = -56i8 as u8; println!("{}, {}, {}", one, at_sign, tw…