2012年6月20日水曜日

Rust 0.2's undocumented function argument prefixes

Maybe someone will find this from google...

around line 1400 of src/libsyntax/print/pprust.rs:

fn mode_to_str(m: ast::mode) -> str {
    alt m {
      ast::expl(ast::by_mutbl_ref) { "&" }
      ast::expl(ast::by_move) { "-" }
      ast::expl(ast::by_ref) { "&&" }
      ast::expl(ast::by_val) { "++" }
      ast::expl(ast::by_copy) { "+" }
      ast::infer(_) { "" }
    }
}

"&&", "++" are not documented yet in both the tutorial and the reference.
"&&" seems like just an immutable version of "&".
"++" will be not useful in most cases -- it is guaranteed to rust compiler choices the better way between by-value and by-reference passing. However this might be useful when we want to declare C-interface functions, which must always use by-value passing.

#############################

Rust言語(ver.0.2, 06/20時点でのgit最新版)です。
関数の引数名のところに、よく意味不明なプレフィックスがついていたので気になって調べてみました。
"&", "-", "+"については公式のチュートリアルに解説があります(なぜかリファレンスには載ってない?)。
"&&"と"++"については、チュートリアルにも載ってなかったのでソースコードから掘り出してみたところ上記のようなソースコードを発見しました。
名前から察するに、
"&&": ただの"&"のconst版 (C++のconst&みたいなの)、強制的に参照渡し
"++": 強制的に値渡し
基本的にRustではコンパイラが勝手に参照渡しと値渡しのうち効率のいい方を使ってくれるはずなのであまり出番がなさそうですが、C言語へのインターフェース関数を書きたい時には強制値渡しの"++"を常時使っておいたほうが安全かもしれません。

0 件のコメント:

コメントを投稿

登録 コメントの投稿 [Atom]

<< ホーム