Rust åºç¤ææ³æéãã¹ã¿ã¼ (rust 0.7 ç·¨)
è¦å (2014/1/25 追è¨)
以ä¸ã®è¨äºã§ãããä»ã¨ãªã£ã¦ã¯éç¨ããªãè¨è¿°ãå¤ãå«ã¾ãã¦ãã¾ãã 0.7 ããç¾å¨ã¾ã§ã«è¡ããã大ããªå¤æ´ã¨ãã¦ã¯ãæãã¤ãã ãã§ã
- è¨èªçµã¿è¾¼ã¿æ©è½ã¨ãã¦ã® managed box ãéæ¨å¥¨ã«ãªã£ã (å°æ¥åé¤ãããã©ã¤ãã©ãªã«ããå®è£ ã¨ç½®ãæãã)
rusti
ãã¼ã«ãåé¤ããã
ã¨ãããã®ãããã¾ãã ãããããæä¸ã®ã³ã¼ãä¾ã¯ã³ã³ãã¤ã«ããéããªããªã£ã¦ãããã¨ã§ãããã ã¾ããä»å¾ã 1.0 ã«åã大ããªå¤æ´ãäºå®ããã¦ãã¾ã (DST ã GC ã®å®è£ ãªã©)ã
æä¸ã®ãè¨èªã®åºæ¬çãªã³ã³ã»ããã«é¢ããé¨åã¯ããããã¦ç¾å¨ã§ãéç¨ããã¨æãã¾ããããã®ä»ã®é¨åã«ã¤ãã¦ã¯ãã¡ãã¨ã¡ã³ããã³ã¹ããã¦ããæç« (å ¬å¼ã®ããã¥ã¡ã³ããªã©) ãåç §ãã¦ãã ããã
以ä¸ããªãªã¸ãã«ã®è¨äºã§ãã
ããã°ç§»è»¢å¾ã®æåã®è¨äºã£ã¨ãããã¨ã§ãæè¿åãããã£ã¦ããããã°ã©ãã³ã°è¨èªã Rust ã®ææ³çãªç¹å¾´ã«ã¤ãã¦ãç°¡åã«ç´¹ä»ãããã¨æãã¾ãã Rust ã¨ããè¨èªã¯é常ã«è¯ããã®ã ã¨æãã®ã§ãæ¯éã¨ãæµè¡ã£ã¦æ¬²ããã§ãï¼ï¼ï¼
åæ¸ã
ã¾ããææ³ã«ã¤ãã¦èª¬æããåã«ãç°¡åã« Rust è¨èªã®ç«ã¡ä½ç½®ãç®æãã¨ããã«ã¤ãã¦ç°¡åã«èª¬æãã¾ãã ã¾ããRust ãå®è¡ããç°å¢ã®æ§ç¯æ¹æ³ããã³å®è¡æ¹æ³ãç°¡åã«èª¬æãã¾ãã®ã§ãæ¬è¨äºãèªãéã¯ãæ¯éãç°å¢ãæ´ãã¦ãå®éã«ã³ã¼ããå®è¡ãã¦ã¿ã¦ãã ããã
Rust ã¨ã¯ï¼
Rust ã¨ã¯ãããã°ã©ãã³ã°è¨èªã®ååã§ããRust ãæ¥æ¬èªã«ããã¨ãããã³ããå½åã®ç±æ¥ã¯åããã¾ããã
Rust ãéçºããã®ã¯ Graydon Hoare ãããå½åã¯å人ã®ããã¸ã§ã¯ãã§ããããç¾å¨ã¯ Mozilla (Firefox/Thunderbird ã®éçºå ) ã®ããã¸ã§ã¯ãã¨ãªã£ã¦ãããGitHub ã§æ´»çºã«éçºãè¡ããã¦ãã¾ãã
- Rust å ¬å¼ãµã¤ã - http://www.rust-lang.org/
- GitHub ã®ãªãã¸ã㪠- https://github.com/mozilla/rust
Rust ã¯ãå®å ¨ (safe) ã§ã並å (concurrent) ã§ãå®ç¨ç (practical) ãã¨ãã 3 ç¹ããã¼ã¯ã¼ãã¨ãã¦æ²ãã¦ãã¾ãã ãµãã¼ããããã©ãã¤ã ã¯ããæç¶ãåããã°ã©ãã³ã°ãããã¡ãã»ã¼ã¸ããã·ã³ã°æ¹å¼ã®ä¸¦åããã°ã©ãã³ã°ããããªãã¸ã§ã¯ãæåããã°ã©ãã³ã°ãããé¢æ°åããã°ã©ãã³ã°ãã¨ä¸éãç¶²ç¾ ãã¦ããã好ã¿ã®ã¹ã¿ã¤ã«ã§ããã°ã©ãã³ã°å¯è½ã§ãã
è¨èªã®ä¸»è¦ãªã¿ã¼ã²ããã¯ãã·ã¹ãã ããã°ã©ãã³ã°é åãã§ãC/C++ 並ã®ããã©ã¼ãã³ã¹ã¨ãå®å ¨æ§ã®ä¸¡ç«ãå®ç¾ãããã¨ãã¦ãã¾ãã å ·ä½çã«ã¯ãRust ãç¨ããã¨ã以ä¸ã®2ã¤ã®è¦ä»¶ãæºãããããã°ã©ã ã (æ¯è¼ç) 容æã«ä½æãããã¨ãå¯è½ã«ãªãã¾ãã
- C/C++ ã®ããã«ãããã¤ã³ã¿ãç¨ããã¡ã¢ãªæä½ããããGC ã«ä¾åããªãã¡ã¢ãªç²å¾ã»è§£æ¾ããªã©ãæ示çãªã¡ã¢ãªç®¡çãå¯è½
- èªåã§ã¡ã¢ãªç®¡çãããè¨èªã®ããã«ãããã³ã°ãªã³ã°ã»ãã¤ã³ã¿ (dangling pointer, ä¸æ£ãªé åãæããã¤ã³ã¿)ãããnull ãã¤ã³ã¿ãããããã«ããªã¼ãããã¡ã¢ãªãªã¼ã¯ã ãªã©å®è¡æã«çºçããªããã¨ãã³ã³ãã¤ã«æã«ä¿è¨¼ããã
Rust ã®å®å ¨æ§ã«é¢ãã¦ã¯ã Rust éçºè ã®ä¸äººã§ãã Tim Chevalier ããã®ãã¬ã¼ã³ãã¼ã·ã§ã³è³æ ã«ã¾ã¨ã¾ã£ã¦ãã¾ãã ãã®ä»ãRust ã®è¨èªä»æ§ã§ç¹å¾´çãªé¨åã以ä¸ã«åæãã¾ãã
- C/C++ 風ã®æ§æ
- ãã¡ã¼ã¹ãã¯ã©ã¹ã®ãªãã¸ã§ã¯ãã¨ãã¦æ±ããé¢æ°
- ã¯ãã¼ã¸ã£
- 代æ°çãã¼ã¿åã¨ããã¿ã¼ã³ãããã«ããåå²
- ãã¼ã«ã«å¤æ°ã®åæ¨è«
- å¼·ãéçåä»ã
- ã¸ã§ããªã¯ã¹
- Haskell ã®åã¯ã©ã¹é¢¨ã® trait ãå©ç¨ãããªãã¸ã§ã¯ãæåããã°ã©ãã³ã°
- 並åã¿ã¹ã¯ãèæ ®ããã¡ã¢ãªç®¡çä½ç³» (ã¿ã¹ã¯ãã¼ã«ã«ãªãã¼ãã¨ã¿ã¹ã¯éã§å ±éãªãã¼ãã®åºå¥)
- ã³ã³ãã¤ã©ã«ãããå¤æ°ã»å¤ã®æå¹ç¯å²ã®éçãªãã§ãã¯
Rust ã§ä½æããã大è¦æ¨¡ãªã½ããã¦ã§ã¢ãã¦ã¯ã Rust ã®ã³ã³ãã¤ã© rustc
ãã Mozilla ãå®é¨çã«éçºä¸ã®ãã©ã¦ã¶åãã¬ã³ããªã³ã°ã¨ã³ã¸ã³ Servo ãåå¨ãã¾ãã
Rust ã®ã¤ã³ã¹ãã¼ã«
2013/07/07 æç¹ã§ã®ææ°ç㯠Rust 0.7 ã§ãã
å ¬å¼ãµã¤ãããã½ã¼ã¹ããã¦ã³ãã¼ããã¦ã³ã³ãã¤ã«ãã¾ãã Windows ã®å ´åã¯ã¤ã³ã¹ãã¼ã©ãç¨æããã¦ãã¾ãã ã¾ã Homebrew ã pacman ã apt-get çã§ããã¤ããªããã®ã¤ã³ã¹ãã¼ã«ãã§ããã¨æãã¾ãã
GitHub ããææ°ã½ã¼ã¹ããã¦ã³ãã¼ããã¦ã³ã³ãã¤ã«ããæ¹æ³ã«ã¤ãã¦ã¯ã以ä¸ãåç §ãã¦ãã ããã
Rust ã®ããã°ã©ã å®è¡
Rust ããã°ã©ã ãå®è¡ããããã«ã¯ã
- ã³ã³ãã¤ã© (
rustc
) ã使ã - REPL (
rusti
) ã使ã
ã¨ãã2ã¤ã®æ¹æ³ãããã¾ããããããã«ã¤ãã¦èª¬æãã¾ãã
rustc
ã®ä½¿ãæ¹
rustc
ã¯ãRust ã®ã½ã¼ã¹ã³ã¼ãããå®è¡ãã¤ããªãçæãã¾ãã
å®è¡ãã¤ããªãä½æããå ´åã¯ãã½ã¼ã¹ã³ã¼ãä¸ã« main
é¢æ°ãå«ãã§ããå¿
è¦ãããã¾ãã
æ¬è¨äºä¸ã®ã³ã¼ãä¾ãå®è¡ããå ´åã¯ã以ä¸ã®ãã¡ã¤ã«ããã³ãã¬ã¼ãã¨ãã¦ç¨ããã¨è¯ãã§ãããã
// extern mod æã use æã¯ããã«æ¸ã fn main() { // ããã«ã³ã¼ãæ¬ä½ãæ¸ã }
ã³ã³ãã¤ã«ã¯ä»¥ä¸ã®ããè¡ãã¾ãã ãªãã Rust ã®ã½ã¼ã¹ã³ã¼ãã®æ¡å¼µå㯠".rs" ã使ããã¦ãã¾ãã
$ rustc ./foo.rs # foo.rs ãã³ã³ãã¤ã«ãã $ ./foo # ã³ã³ãã¤ã«ãã¦çæããããã¤ã㪠foo (windows ãªã foo.exe) ãå®è¡ãã
ã³ã³ãã¤ã«ã¨å®è¡ãåæã«è¡ã£ã¦ãããã³ãã³ã rust run
ãåå¨ãã¾ãã
$ rust run ./foo.rs # foo.rs ãã³ã³ãã¤ã«ããå®è¡ãã
Unix ç³»ã·ã¹ãã ã®å ´åã¯ã以ä¸ã®ããã«ã½ã¼ã¹ã³ã¼ãã®å é ã« shebang ãæ¸ãã¦ããã¡ã¤ã«ã«å®è¡å±æ§ (x) ãä¸ããã°ã ã¹ã¯ãªããã®ããã«å®è¡ãããã¨ãå¯è½ã§ãã
#!rust run fn main() { println("Hello, World"); }
$ chmod +x foo.rs $ ./foo.rs Hello, World
rusti
ã®ä½¿ãæ¹
rusti
㯠REPL (対話çå®è¡ç°å¢) ãªã®ã§ãäºåã«ã½ã¼ã¹ã³ã¼ãã®ãã¡ã¤ã«ãç¨æããå¿
è¦ã¯ããã¾ããã
以ä¸ã®æ§ã«èµ·å & ã½ã¼ã¹ã³ã¼ãã®è©ä¾¡ãè¡ãã¾ãã
$ rusti WARNING: The Rust REPL is experimental and may be unstable. If you encounter problems, please use the compiler instead. Type :help for help. rusti> 1 + 2 # ã³ã¼ããå ¥å 3 # è©ä¾¡çµæãåºåããã rusti> :exit # rusti ãçµäºãã
è¤æ°è¡ã«ãããã½ã¼ã¹ã³ã¼ããå
¥åããå ´åã¯ã:{
㨠:}
ã§å²ã¿ã¾ãã
rusti> :{ rusti| 1 + rusti| 2 + rusti| 3 + rusti| 4 rusti| :} 10
rusti
ã³ãã³ãèµ·åæã®ã¡ãã»ã¼ã¸ããåããã¨ãããrusti
ã¯å®é¨çã»ä¸å®å®ãªæ©è½ãªãããrustc
ã§ã¯æ£ããåä½ããã¯ãã®ã³ã¼ããåä½ããªããªã©ã®åé¡ãçºçããå¯è½æ§ãããã¾ãã
æ¬è¨äºã®ãµã³ãã«ã³ã¼ãã«ã¤ãã¦ã¯ rusti
ã§ãåä½ç¢ºèªãè¡ã£ã¦ããã¾ãããä½ãåé¡ãçºçããå ´å㯠rustc
ã使ã£ã¦ã³ã¼ããå®è¡ãã¦ã¿ããã¨ããªã¹ã¹ã¡ãã¾ãã
注æäºé
æ¬è¨äºä¸ã®ã½ã¼ã¹ã³ã¼ãã¯ã2013/7/3 ã«ãªãªã¼ã¹ããã rust 0.7 ã§åä½ç¢ºèªãã¦ãã¾ãã
$ rust -v rust 0.7 host: x86_64-unknown-linux-gnu
rust ã¯ç¾å¨ã¢ã«ãã¡çã®ãªãªã¼ã¹éç¨ã«ãããè¨èªä»æ§ãã©ã¤ãã©ãªã¸ã®ç ´å£çå¤æ´ãè¡ããã¦ããçã£æä¸ã§ã æ¬è¨äºã«æ¸ãããå 容ãå°æ¥ã«æ¸¡ã£ã¦éç¨ããªãå¯è½æ§ãããã¾ãã (0.6 ãªãªã¼ã¹ä»¥éã¯ã1.0 ã®ãªãªã¼ã¹ã¾ã§ãè¨èªä»æ§ã¸ã®å¤§ããªå¤æ´ã¯è¡ããªãæ¹éã®ããã§ã¯ããã¾ããããã)
æ¬è¨äºã«ã¤ãã¦ãä»å¾ã®ãªãªã¼ã¹ã«ä½µãã¦éææ¹è¨ãã¦ããäºå®ã§ãããåé¡ãçºçããå ´åã¯ã³ã¡ã³ãæ¬çã§ãææããã ããã¨å¤§å¤å©ããã¾ãã ã¾ããçè ãææ¡ãã¦ããéãã§ä»å¾ä»æ§ãå¤æ´ããããã¨ãäºæ³ããããã¤ã³ãã«ã¤ãã¦ã¯ãé©å®æ³¨éãå ¥ãã¦ããã¾ãã
ãã¦ãåç½®ããé·ããªãã¾ããã次ã®ç¯ãã rust ã®ææ³ã«ã¤ãã¦èª¬æãã¾ãã
åºç¤
Rust ã®ããã°ã©ã ã¯ã以ä¸ã®ãããªè¦ãç®ããã¦ãã¾ãã
fn main() { let mut cond = false; loop { if cond { break; } cond = fun(); } }
if
ã®å¾ã®æ¬å¼§ã®æç¡ããã¼ã¯ã¼ãã®éãçã¯ããã¾ãããRust ã¯ãä¸æ¬å¼§ {}
ãå©ç¨ãããC 風ã®æ§æãæ¡ç¨ãã¦ãã¾ãã
ã³ã¡ã³ã
ã³ã¡ã³ãã¯ãC ç³»åè¨èªã¨åãã§ãã
//
, /* */
ã®ä»£ããã« ///
, /** */
ãå©ç¨ããã¨ãããã¥ã¡ã³ãã³ã¡ã³ãã¨ãªãã¾ãã
// ä¸è¡ã³ã¡ã³ã /* è¤æ° è¡ ã³ã¡ã³ã */
端æ«ã¸ã®åºå
ããã°ã©ã ããæ¨æºåºåã«æååãåºåããã«ã¯ãprint
é¢æ°ã println
é¢æ°ãå©ç¨ãã¾ãã
print
é¢æ°ã¯æå®ãããæååããã®ã¾ã¾åºåãã¾ãããprintln
ã¯æ«å°¾ã«æ¹è¡ãä»ä¸ãã¦åºåãã¾ãã
print("Hello World\n"); println("Hello World");
fmt!
æ§ææ¡å¼µ (syntax extension) ãå©ç¨ãããã¨ã§ãC ã® printf
ã®ãããªãã©ã¼ãããä»ãåºåãå©ç¨ã§ãã¾ãã
println(fmt!("%d + %d = %d", 1, 2, 1+2)); //=> 1 + 2 = 3
fmt!
ã§å©ç¨å¯è½ãªãã©ã¼ãããæå®åã¯ä»¥ä¸ã®éãã
println(fmt!("%d %i", 123, 456)); // %d, %i: 符å·ä»ãæ´æ° => 123 456 println(fmt!("%u", 123)); // %u: 符å·ç¡ãæ´æ° => 123 println(fmt!("%s", "string")); // %s: æåå => string println(fmt!("%c", 'c')); // %c: æå => c println(fmt!("%b", true)); // %b: ãã¼ã«å => true println(fmt!("%x", 127)); // %x: 16é²æ´æ° (å°æå) => 7f println(fmt!("%X", 127)); // %X: 16é²æ´æ° (大æå) => 7F println(fmt!("%o", 10)); // %o: 8é²æ´æ° => 12 println(fmt!("%f", 1.23)); // %f: æµ®åå°æ° => 1.23 println(fmt!("%?", ("asd", 123, true))); // %?: ä»»æã®å (ããã§ã¯ã¿ãã«å) => ("asd", 123, true)
%10d
ã®ããã«æ¡æ°ãæå®ãããã¨ãå¯è½ã§ãã
æå®å¯è½ãªå
容ã«ã¤ãã¦ã¯ã
fmt!
ã®ãã¹ãã»ãã
ã«ç¶²ç¾
çã«æ¸ããã¦ãã¾ãã
fmt!
㯠C 㮠printf
ã¨ã¯ç°ãªãããã©ã¼ãããæåã¨å¼æ°ã®ä¸æ´å (å¼æ°ã®æ°ã®ä¸ä¸è´ããåã®ä¸ä¸è´) ãã³ã³ãã¤ã«æã«ãã§ãã¯ãã¦ããã¾ãã
ç´ æµã§ããï¼
fmt!("%d", "asdf"); // åãä¸ä¸è´ (%d ã«å¯¾ããæåå)
<anon>:28:12: 28:18 error: mismatched types: expected `int` but found `&'static str` (expected int but found &'static str) <anon>:28 fmt!("%d" , "asdf"); ^~~~~~ note: in expansion of fmt! <anon>:28:0: 28:20 note: expansion site
fmt!("%d %d", 123); // å¼æ°ã®æ°ãä¸ä¸è´ (%d 2åã«å¯¾ããå¼æ°1å)
<anon>:28:0: 28:20 error: not enough arguments to fmt! for the given format string <anon>:28 fmt!("%d %d" , 123); ^~~~~~~~~~~~~~~~~~~~
ãªããç´é¢ (?) ã®é½åã§ãä¸è¨ã¨ã©ã¼ã¡ãã»ã¼ã¸ã¯æ®éè²ã®æåã¨ãã¦è¨è¼ãã¦ãã¾ãããå®éã¯ã«ã©ãã«ãªè¡¨ç¤º (error ã¯èµ¤, warning ã¯é», note ã¯ç·) ã¨ãªãã¾ãã
å¤æ°ã®å®£è¨
ãã¼ã«ã«å¤æ°ã®å®£è¨
é¢æ°å
é¨ã§å©ç¨ãããã¼ã«ã«å¤æ°ã¯ãlet
ã§å®£è¨ãã¾ãã
let foo: int = 123;
let
ã§å®ç¾©ããå¤æ°ã¯ immutable (å¤æ´ä¸å¯) ã§ããå¤ãå¤æ´ãããã¨ããã¨ãã³ã³ãã¤ã«ã¨ã©ã¼ã¨ãªãã¾ãã
let foo: int = 123; foo = 1;
<anon>:39:0: 39:3 error: re-assignment of immutable variable `foo` <anon>:39 foo = 1; ^~~ <anon>:33:4: 33:7 note: prior assignment occurs here <anon>:33 let foo: int = { ^~~
å®ç¾©èªã«å¤ãå¤æ´å¯è½ãªå¤æ°ã宣è¨ããã«ã¯ãlet mut
ãå©ç¨ãã¦ãã ããã
let mut bar: bool = true; // mutable (å¤æ´å¯è½) ãªå¤æ°ãå®£è¨ bar = false;
ãã¼ã«ã«å¤æ°ã¯åæ¨è«ããããããä¸è¨ã®ä¾ã®åæå®ãçç¥ãããã¨ãå¯è½ã§ãã
let foo = 123; let bar = true;
static
å¤æ°ã®å®£è¨
let
ã¯é¢æ°å
é¨ã§ã®ã¿å©ç¨ã§ãã¾ããè¤æ°ã®é¢æ°ããå©ç¨å¯è½ãªå®æ°ã宣è¨ããã«ã¯ãstatic
ã使ãã¾ãã
let
ã¨éããåæ¨è«ã¯ããã¾ããã
static foo: int = 123;
mutable 㪠static
å¤æ°ãå®ç¾©ãããã¨ãå¯è½ã§ãã
ã°ãã¼ãã«ã§ mutable ãªå¤æ°ã®å©ç¨ã¯ å±éº ãªã®ã§ãå©ç¨ããå ´åã¯å±éºãªæä½ãè¡ããã¨ã示ã unsafe
ãããã¯ã§å©ç¨ç®æãå²ãå¿
è¦ãããã¾ãã
static mut foo: int = 123; // å©ç¨ããå ´å㯠unsafe 宣è¨ãå¿ è¦ unsafe { foo = 1; } let bar = unsafe { foo }; // 以ä¸ã¯ã¨ã©ã¼ foo = 1;
å¼ã¨ã»ãã³ãã³
C 㨠Rust ã®ææ³ã®å¤§ããªéãã¨ãã¦ãC ã§ã¯ãæãã¨ãã¦æ±ãããæ§æã®å¤ãããRust ã§ã¯ãå¼ãã¨ãã¦æ±ããããã¨ãæãããã¾ãã æ¡ä»¶ã«å¿ãã¦é©åãªå¤ãå¤æ°ã«è¨å®ãããå ´åãC ãå¦ãã 人ãæ®éã«æ¸ãã¨ã以ä¸ã®ãããªæ¸ãæ¹ã«ãªãã¨æãã¾ãã
let price; if weight > 100 { price = 10; } else if weight > 50 { price = 5; } else { price = 1; }
Rust ã® if
ã¯ãå¼ããªã®ã§ã以ä¸ã®ããã«æ¸ããã¨ã§ price
ã®ç¹°ãè¿ããé¿ãããã¨ãå¯è½ã§ãã
let price = if weight > 100 { 10 } else if weight > 50 { 5 } else { 1 };
if
ã ãã§ãªãããããã¯ãå¼ã¨ãã¦æ±ããã¾ãã
ãããã¯ã®è¿ãå¤ã¯ããããã¯å
ã®æå¾ã®æã®å¤ã§ãã
let foo = { let a = 1; let b = 2; a + b }; // foo == 3
æå¾ã®å¼ã«ã¯ã»ãã³ãã³ãã¤ãã¦ããªããã¨ã«æ³¨æãã¦ãã ããã
å¼ã®æ«å°¾ã«ã»ãã³ãã³ãä»ããã¨ããã®å¼ã®å¤ã¯ç¡è¦ããã()
ã¨ããç¹å¥ãªå¤ãè¿å´ããã¾ãã
let a = { 123 }; // a == 123 let b = { 123; }; // b == ()
åºæ¬çãªå
bool å
çã表ã true
ã¨ãå½ã表ã false
ã®2ã¤ã®å¤ã®ã¿ãã¨ããåã§ãã
以ä¸ã®æ¼ç®ãåå¨ãã¾ãã
true && false //=> false true || false //=> true !true //=> false
C ãªã©ã¨åããã&&
, ||
ã¯ç絡è©ä¾¡ã§ãã
æ°å¤
è¨èªçµã¿è¾¼ã¿ã®æ°å¤åã¯ã符å·ä»ãæ´æ°ããã符å·ãªãæ´æ°ãããæµ®åå°æ°ãã®3種é¡ãããã¾ãã ããããã®ç¨®é¡ã«ã¤ãã¦ããµã¤ãºãç°ãªãåãããã¤ãåå¨ãã¾ãã
- 符å·ä»ãæ´æ°
int
: ãã·ã³ã®ã¬ã¸ã¹ã¿ãµã¤ãºã¨åããµã¤ãº (32bit OS ãªã 32bit, 64bit OS ãªã 64bit)i8
,i16
,i32
,i64
: ååã®æ°å¤ã¨åããµã¤ãº (i8 ãªã 8bit)
- 符å·ãªãæ´æ°
uint
: ãã·ã³ã®ã¬ã¸ã¹ã¿ãµã¤ãºã¨åããµã¤ãºu8
,u16
,u32
,u64
: ååã®æ°å¤ã¨åããµã¤ãº
- æµ®åå°æ°
float
: f64 ã¨åã (0.7 æç¹ãä»å¾å¤æ´ã®å¯è½æ§ãããï¼)f32
,f64
: ååã®æ°å¤ã¨åããµã¤ãº
ããããã®æ°å¤åã«å¯¾å¿ãããªãã©ã«ãåå¨ãã¾ãã
- 符å·ä»ãæ´æ°ã®å ´å
int
: æ°å¤ã®æ«å°¾ã«i
ãã¤ãã (ä¾:12i
)i8
,i16
,i32
,i64
: æ°å¤ã®æ«å°¾ã«iXX
ãã¤ãã (ä¾:12i8
,-12i32
)
- 符å·ãªãæ´æ°ã®å ´å
uint
: æ°å¤ã®æ«å°¾ã«i
ãã¤ãã (ä¾:12u
)u8
,u16
,u32
,u64
: æ°å¤ã®æ«å°¾ã«uXX
ãã¤ãã (ä¾:12u8
,12u32
)
- æµ®åå°æ°
float
: æ°å¤ã®æ«å°¾ã«f
ãã¤ãã (ä¾:1.2f
,1f
)f32
,f64
: æ°å¤ã®æ«å°¾ã«fXX
ãã¤ãã (ä¾:1.2f32
,-1f64
)
i
, i32
, u64
çã®åã表ããµãã£ãã¯ã¹ãçç¥ããå ´åãå½è©²ãªãã©ã«ã®å©ç¨ããæ¹ããã©ã®åãèªåçã«æ¨è«ããã¾ãã
let x: uint = 12; // x 㯠uint åã® 12 let y: float = 1.2; // y 㯠float åã® 1.2
æ´æ°ã®ãªãã©ã«ãæµ®åå°æ°ã¨ãã¦è§£éãããããæµ®åå°æ°ã®ãªãã©ã«ãæ´æ°ã¨ãã¦è§£éããããã¨ã¯ããã¾ããã
let x: float = 12; // ã¨ã©ã¼ let y: uint = 1.2; // ã¨ã©ã¼
ãªããããã§ç´¹ä»ãã float
åã¯ãå°æ¥ç¡ããªãããããã¾ãã (Github ã®é¢é£ Issue)
æ°å¤ã®æ¼ç®
æ°å¤ã«é¢ããæ¼ç®ã§ãã
ååæ¼ç®
1 + 2 //=> 3 1 - 2 //=> -1 1 * 2 //=> 2 1 / 2 //=> 0 5 % 2 //=> 1 -(5) //=> -5 1.0 + 2.5 //=> 3.5 1.0 - 2.5 //=> -1.5 1.0 * 2.5 //=> 2.5 1.0 / 2.5 //=> 0.4 -(5.0) //=> -5
æ¯è¼æ¼ç®å
1 >= 2 //=> false 1 > 2 //=> false 1 == 2 //=> false 1 != 2 //=> true 1 < 2 //=> true 1 <= 2 //=> true
ãããæ¼ç® (æ´æ°ã®ã¿)
3 << 2 //=> 12 16 >> 2 //=> 4 0xff & 0x01 //=> 1 (è«çç©) 0xff | 0x01 //=> 255 (è«çå) 0xff ^ 0x01 //=> 254 (æä»çè«çå) !(-100) //=> 99 (ãããå転ãC ã® ~)
ä»£å ¥æ¼ç®å (ä¸é¨)
let mut a = 1; // a == 1 a += 3; // a == 4 a *= 2; // a == 8 a ^= 1; // a == 9
ã¤ã³ã¯ãªã¡ã³ãæ¼ç®å ++
ããã¯ãªã¡ã³ãæ¼ç®å --
ã¯ããã¾ããã
+= 1
, -= 1
ã使ãã¾ãã
ç°ãªãåã¨ã®æ¼ç®
2é æ¼ç®åã«ããæ¼ç®ã¯ãåãæ°å¤åå士ã®å ´åã®ã¿è¡ãã¾ããç°ãªãåå士ã®æ¼ç®ã¯ã¨ã©ã¼ã¨ãªãã¾ããæé»ã®åå¤æã¯ããã¾ããã
1.2 + 1
<anon>:29:6: 29:7 error: mismatched types: expected `<VF0>` but found `<VI0>` (expected floating-point variable but found integral variable) <anon>:29 1.2 + 1; ^
ãµã¤ãºã®éãæ´æ°å/æµ®åå°æ°åå士ã®æ¼ç®ãã¨ã©ã¼ã§ãã
1u32 + 1u64
<anon>:29:7: 29:11 error: mismatched types: expected `u32` but found `u64` (expected u32 but found u64) <anon>:29 1u32 + 1u64; ^~~~
ç°ãªãåå士ã®æ¼ç®ãè¡ãå ´åã¯ãas
ã使ã£ã¦æ示çã«ãã£ã¹ããã¾ãããã
let sum = 123.4; let count = 10; let avg = sum / (count as float); println(fmt!("%f", avg)); //=> 12.34
Rust ã®ã¡ã¢ãªã¢ãã«
Rust ã«ã¯4種é¡ã®ãã¤ã³ã¿å ~T
, @T
, &T
, *T
ãããã¾ãã
Owned box (~T
)
Owned box ã¯ããã¼ãä¸ã«ç²å¾ãããé åã®äºãæãã¾ãã
Owned box ã¯ã C++ ã® unique_ptr
ã®ããã«ãåä¸ã®å¤æ°ã®ã¿ãææ権ãæã¡ã¾ãã
let x = ~5; // 5 ããã¼ãä¸ã«ç¢ºä¿ let y = x; // y ã«ææ権ã移ã // *y = 3; // y 㯠immutable ãªã®ã§ãå¤æ´ä¸å¯è½ let mut z = y; // z ã«ææ権ã移ã *z = 3; // z 㯠mutable ãªã®ã§ãå¤æ´å¯è½ // *y + 3; // y ã«æ ¼ç´ããã¦ããå¤ã¯ z ã«ææ権ã移ã£ã¦ãããããy ããã¯ã¢ã¯ã»ã¹ã§ããªã
Owned box ã¨ãã¦ç²å¾ãããé åã¯ãé åã¸ã®åç §ãç¡ããªã£ãæç¹ã§è§£æ¾ããã¾ãã ãã®è§£æ¾ã¿ã¤ãã³ã°ã¯ãã½ã¼ã¹ã³ã¼ãããéçã«è§£æå¯è½ã§ãã¬ãã¼ã¸ã³ã¬ã¯ã¿ã¯å¿ è¦ããã¾ããã
Managed box (@T
)
Managed box ã¯ããã¼ãä¸ã«ç²å¾ãããé åã®äºãæãã¾ãã
Managed box ã¯ãC++ ã® shared_ptr
ã®ããã«ãè¤æ°ã®å¤æ°ããåä¸ã®é åãæããã¨ãå¯è½ã§ãã
let x = @5; // 5 ããã¼ãä¸ã«ç¢ºä¿ let y = x; // x 㨠y ã¯åãé åãæã
Managed box ã¨ãã¦ç²å¾ãããé åã¯ãé åã¸ã®åç §ãç¡ããªã£ãæç¹ã§è§£æ¾ããã¾ãã ãã®éæ¾å¦çã¯ãã¬ãã¼ã¸ã³ã¬ã¯ã¿ã«ããè¡ããã¾ãã
Borrowed pointer (&T
)
Borrowed pointer ã¯ãC++ ã®åç §åã®ãããªãã®ã§ãä»»æã®å¤ãåç §ããããã«ç¨ãããã¨ãã§ãã¾ãã ã¾ããæã示ããå¤ãç¡å¹ã«ãªããªããã¨ããã³ã³ãã¤ã©ãéçã«ä¿è¨¼ãã¾ãã
let ptr; // uint åã® borrowed pointer ãå®£è¨ { let x = 1; // ãããã¯å ã§ã®ã¿æå¹ãªå¤æ° x ã®å®£è¨ ptr = &x; // x ãæã borrowed pointer ã ptr ã«ä»£å ¥ } println(fmt!("%?", ptr)); // ãããã¯ã®å¤ã§ x ã®å¤ãåç §
<anon>:35:19: 35:21 error: borrowed value does not live long enough <anon>:35 { let x = 1; ptr = &x; println(fmt!("%?" , ptr)); } ^~ <anon>:25:22: 1:0 note: borrowed pointer must be valid for the block at 25:22... <anon>:35:0: 35:51 note: ...but borrowed value is only valid for the block at 35:0 <anon>:35 { let x = 1; ptr = &x; println(fmt!("%?" , ptr)); } ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ä¸è¨ããã°ã©ã ã¯ããããã¯å
ã§ã®ã¿æå¹ãªå¤æ° x
ãããããã¯ãæããæç¹ã§åç
§ãããã¨ãã¦ãã¾ãã
Rust ã³ã³ãã¤ã©ã¯ããã®ãããªä¸æ£ãªã¡ã¢ãªã¢ã¯ã»ã¹ãæ¤ç¥ããã¨ã©ã¼ã¨ãã¦ããã¾ãã
ãã¼ãã®ç¨®é¡
Rust ã«ã¯ exchange heap ã¨ã local heap ã¨ãã2種é¡ã®ãã¼ããç¨æããã¦ãã¾ãã exchange heap ä¸ã®å¤ã¯è¤æ°ã®ã¿ã¹ã¯ (ã¹ã¬ããã®ãããªãã®) ããåæã«ã¢ã¯ã»ã¹å¯è½ã§ããã local heap ä¸ã®å¤ã¯åä¸ã¿ã¹ã¯ããããã¢ã¯ã»ã¹ã§ãã¾ãã (ã¿ã¹ã¯æ¯ã«åºæã® local heap ãããã¾ã)ã
owned box 㯠exchange heap ä¸ã«ç²å¾ãããmanaged box 㯠local heap ä¸ã«ç²å¾ããã¾ãã ããªãã¡ãã¿ã¹ã¯éã§ãã¼ã¿ãããã¨ãããå ´åã¯ãå¿ ã owned box ã«ããªããã°ãªãã¾ããã ãã®ä»æ§ã«ããã以ä¸ãå®ç¾ã§ãã¾ãã
- è¤æ°ã®ã¿ã¹ã¯ãåæã«åä¸ã®ã¡ã¢ãªé åã«ã¢ã¯ã»ã¹ããªã (ã¿ã¹ã¯æ¯ã®ã¡ã¢ãªé åãå®å ¨ã«åé¢)
- ã¿ã¹ã¯éã§ãã¼ã¿ãåã渡ãéã«ããã¼ã¿å ¨ä½ãã³ãã¼ããªãã¦è¯ã (Owned box ã®ãã¤ã³ã¿ã®å¤ã渡ãã ãã§è¯ã)
æååã¨é å
æååã»æå
Rust ã®æåååã¯ãã©ã®é åã«å¤ãæ ¼ç´ãããã«å¿ãã¦ã&str
(ã¹ã¿ãã¯ä¸ã«ç¢ºä¿), ~str
(Exchange heap ä¸ã«ç¢ºä¿), @str
(local heap ä¸ã«ç¢ºä¿) ã®3種é¡ãããã¾ãã
ããããã®éãã«ã¤ãã¦ã¯ãå¾ã®ç¯ã§èª¬æãã¾ãã
æåå㯠char
ã§ãã
æååãªãã©ã«ãæåãªãã©ã«ã¯ä»¥ä¸ã®æ§ã«æ¸ãã¾ãã
let a = ~"string"; // ~str åã®æåå let b = @"string"; // @str åã®æåå let c = "string"; // &str åã®æåå let d = 'c'; // æå (char å)
ã¨ã¹ã±ã¼ãã·ã¼ã±ã³ã¹ã使ãã¾ãã
let new_line = "aaa\nbbb";
æååã®æä½
å種æååæä½ã§ã
çµå
"aaa" + "bbb" // => "aaabbb" ["aaa", "bbb", "ccc"].connect(",") // => "aaa,bbb,ccc" ["aaa", "bbb", "ccc"].concat() // => "aaabbbccc"
åå²
æåååå²é¢æ°ã¯ãä»ã®è¨èªã®ããã«é åãè¿ãã®ã§ã¯ç¡ããã¤ãã¬ã¼ã¿ãè¿ãã¾ãã ä½è¨ãªã¡ã¢ãªç²å¾ã極åé¿ããã¨ããã Rust ã®å¹çæ§ã«å¯¾ããææ°è¾¼ã¿ãæãããã¾ãã
let mut iter = "aaa,bbb,ccc".split_iter(',') //=> ã¤ãã¬ã¼ã¿ iter.collect::<~[&str]> //=> ~["aaa", "bbb", "ccc"] ã¤ãã¬ã¼ã¿ããé åãä½æ let mut iter = "aaa<>bbb<>ccc".split_str_iter("<>") //=> ã¤ãã¬ã¼ã¿ iter.collect::<~[&str]> //=> ~["aaa", "bbb", "ccc"] ã¤ãã¬ã¼ã¿ããé åãä½æ
é·ã
"aaa".len() //=> 3 "æ¥æ¬èª".len() //=> 9 (ãã¤ãæ°) "æ¥æ¬èª".char_len() //=> 3 (æåæ°)
åãåºã
"abcd".slice(0, 2) //=> "ab" "æ¥æ¬èª".slice_chars(0, 2) //=> "æ¥æ¬"
æ¤ç´¢
// æååã®è¦ã¤ãã£ãç¯å²ã®ã¤ãã¬ã¼ã¿ãè¿ã "abcd efdh abcd".matches_index_iter("cd").collect::<~[(uint, uint)]>() //=> ~[(2, 4), (12, 14)] // æåã«è¦ã¤ãã£ãæåã®ä½ç½®ãè¿ã "abcd abcd".find('b') //=> Some(2) "abcd abcd".find('x') //=> None // æå¾ã«è¦ã¤ãã£ãæåã®ä½ç½®ãè¿ã "abcd abcd".rfind('b') //=> Some(6) "abcd abcd".rfind('x') //=> None // æåã«è¦ã¤ãã£ãæååã®ä½ç½®ãè¿ã "abcd abcd".find_str("cd") //=> Some(2) "abcd abcd".find_str("xx") //=> None
é å (Vector)
Rust ã®é
ååã¯ãå¯å¤é·ã®é
åã¨åºå®é·ã®é
åã®2種é¡ãããã¾ãã
å¯å¤é·ã®é
åã¯ãè¦ç´ ã®åã T
ã¨ããã¨ã&[T]
, ~[T]
, @[T]
ã®3種é¡ã«æ´ã«åé¡ããã¾ãã
int
ã®é
å㯠&[int]
ã ~[int]
, @[int]
ãªã©ã¨æ¸ãã¾ãã
åºå®é·ã®é
åã¯ã [T, ..ãN]
ã¨æ¸ããã¾ããN
ã¯è¦ç´ æ°ã§ããè¦ç´ æ°3ã® int
ã®é
å㯠[int, ..ã3]
ã¨æ¸ãã¾ãã
let a = ~[1, 2, 3]; // ~[int] å let b = @["a", "b", "c"]; // @[&str] å let c = &[1.0, 2.0, 3.0]; // &[float] å let d = [1, 2, 3]; // [int, .. 3] å
é åã«ç°ãªãåã®è¦ç´ ãæ··ãããã¨ã¯ã§ãã¾ããã è¤æ°ã®åãæ··ãããå ´åã¯ãå¾è¿°ã®ã¿ãã«ã使ãã¾ãã
[1i32, 1.0f, "aaa"]
<anon>:29:7: 29:11 error: mismatched types: expected `i32` but found `float` (expected i32 but found float) <anon>:29 [1i32, 1.0f, "aaa"]; ^~~~ <anon>:29:13: 29:18 error: mismatched types: expected `i32` but found `&'static str` (expected i32 but found &'static str) <anon>:29 [1i32, 1.0f, "aaa"]; ^~~~~
é åã®æä½
å種é åæä½ã§ã
é åã®çææ¹æ³
use std::vec; // è¦ç´ æ°é¢æ°ããçæ vec::from_fn(4, |i| i + 1) //=> ~[1, 2, 3, 4] // è¦ç´ æ°ã¨è¦ç´ ããçæ vec::from_elem(4, "123") //=> ~["123", "123", "123", "123"]
é åã®åç §ã¨ä»£å ¥
let mut v = ~[1, 2, 3]; let n = v[2]; // n == 3 (é åã®æ·»ãå㯠0 ããå§ã¾ã) v[0] = 3; // v == ~[3, 2, 3]
è¦ç´ ã®åæ°
~[1, 2, 3].len() //=> 3
å é ã®è¦ç´ ãåãåºã
let mut v = ~[1, 2, 3]; v.shift(); //=> 1, v == ~[2, 3]
å é ã«è¦ç´ ã追å
let mut v = ~[1, 2, 3]; v.unshift(4); // v == ~[4, 1, 2, 3]
æ«å°¾ã®è¦ç´ ãåãåºã
let mut v = ~[1, 2, 3]; v.pop() //=> 3, v == ~[1, 2]
æ«å°¾ã«è¦ç´ ã追å
let mut v = ~[1, 2, 3]; v.push(4); // v == ~[1, 2, 3, 4]
é åã®ä¸é¨ã¸ã®åç § (slice) ãå¾ã
let v = ~[1, 2, 3, 4]; let s = v.slice(1, 3); // s == &[2, 3] s.len() //=> 2 let mut v = ~[1, 2, 3, 4]; let mut s = v.mut_slice(1, 3); // s == &mut [2, 3] s[0] = 5; //=> v == ~[1, 5, 3, 4]
ããã
ä»ã®è¨èªã§ã¯ããã·ã¥ãé£æ³é åã¨å¼ã°ãããã®ã§ãã Rust ã®ãããåã¯è¨èªçµã¿è¾¼ã¿ã§ã¯ãªããæ¨æºã©ã¤ãã©ãªã§å®ç¾©ããã¦ãããã®ã§ãã
ããã
ãã¼ã¨å¤ã®ãã¢ã§ããå㯠HashMap<K, V>
ã§ãã
K
ã¯ãã¼ã®åã V
ã¯å¤ã®åã§ãã
ãããã®çæ
new
ã¨ããååã®éçã¡ã½ãããå¼ã³åºãã¾ã (éçã¡ã½ããã«ã¤ãã¦ã¯ãå¾ã§èª¬æãã¾ã)ã
use std::hashmap::HashMap; let mut map = HashMap::new::<uint, ~str>(); // ãã¼ã uint, å¤ã ~str ã®ããã
å¤ã®è¨å®
map.insert(0, ~"zero"); map.insert(1, ~"one");
å¤ã®åç §
map.find(&0) //=> Some(&~"zero") map.find(&1) //=> Some(&~"one") map.find(&10) //=> None map.get(&0) //=> &~"zero" map.get(&10) // ç°å¸¸çµäº
ãã¼ã®åå¨ç¢ºèª
map.contains_key(&0) //=> true map.contains_key(&10) //=> true
ãã¼ã®åé¤
map.remove(&0) //=> true (è¦ç´ ãåå¨ããå ´å true ãè¿ã) map.remove(&10) //=> false
å¶å¾¡æ§æ
æ¡ä»¶åå²
æ¢ã«ä½åº¦ãç»å ´ãã¦ãã¾ãããæ¡ä»¶åå²ã«ã¯ if
æãç¨ãã¾ãã
cond
ã®é¨åã«ã¯ çå½å¤ (bool
åã®å¤) ããã¤å¼ãæ¸ãã¾ãã
C ã¨éããint
åçã®æ°å¤ãçå½å¤ã¨ãã¦å©ç¨ãããã¨ã¯ã§ãã¾ããã
ã¾ããC çã§ã¯ if
ã®å¾ã«æ¬å¼§ (
)
ãå¿
è¦ã§ãããRust ã§ã¯ä¸è¦ã§ãã
if cond { foo(); } if cond { foo(); } else { bar(); } if cond1 { foo(); } else if cond2 { bar(); } else { baz(); }
ãã¿ã¼ã³ããã
Rust ã® match
æ§æã¯ãC ã® switch
æãæ¡å¼µãããã®ã§ããã¨è¨ãã¾ãã
C ã® switch
ã§ã¯ãcase
ã®å¾ã«å®æ°ããæ¸ãã¾ããããRust ã§ã¯ããã¿ã¼ã³ããæ¸ããã¨ãã§ãã
ããä¸è¬çãªæ¡ä»¶åå²ãå¯è½ã§ãã
match number { 0 => println("zero"), // 0 ã®å ´å 1 | 2 => println("one or two"), // 1 ã¾ã㯠2 ã®å ´å ("|" ã¯ãããããã«ããããããã¨ã表ã) 3..10 => println("three to ten"), // 3 以ä¸10以ä¸ã®å ´å (".." ã¯ãç¯å²ã表ã) _ => println("something else") // ä¸è¨ä»¥å¤ã®å ´å ("_" ã¯ãä»»æã®å¤ã«ããããã) }
å =>
ã®å·¦å´ã«æ¸ããã¦ãããã®ãããã¿ã¼ã³ãã§ãã
match
æ§æã§ã¯ã渡ãããå¤ (ä¸è¨ã®ä¾ã§ã¯ number
) ãããã¿ã¼ã³ãã«å½ã¦ã¯ã¾ããã©ããé çªã«ãã§ãã¯ãã¦ããã
æåã«å½ã¦ã¯ã¾ã£ããã®ã«å¯¾å¿ããå¼ (=>
ã®å³å´ã«æ¸ããã¦ãããã®) ãå®è¡ããã¾ãã
C ã® switch
ã¨ã¯ç°ãªãã"fall through" ã§ã¯ããã¾ããã
=>
ã®å¾ã«ã¯ãããã¯ãæ¸ããã¨ãã§ãã¾ãã
ãã®å ´åãåå¼ã®æ«å°¾ã® ,
ã¯çç¥ãããã¨ãã§ãã¾ãã
match number { 0 => { println("zero") } 1 | 2 => { println("one or two") } 3..10 => { println("three to ten") } _ => { println("something else") } }
ãã¿ã¼ã³ã®ç¶²ç¾ æ§
ãã¿ã¼ã³ã¯ããã¹ã¦ã®æãå¾ãå¤ã網ç¾
ããããã«æ¸ããªããã°ãªãã¾ããã
以ä¸ã®ããã«ãä¸é¨ã®æ°å¤ã«å¯¾å¿ãããã¿ã¼ã³ãåå¨ããªã match
ãæ¸ããå ´åãã³ã³ãã¤ã«ã¨ã©ã¼ã¨ãªãã¾ãã
let n: uint = fun(); // n 㯠uint å match n { 0 => "zero", 1 => "one" // 2 以ä¸ã®å¤ã«å¯¾å¿ãããã¿ã¼ã³ãªã }
<anon>:43:0: 43:35 error: non-exhaustive patterns <anon>:43 match n { 0 => "zero", 1 => "one" } ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ãã®ä»æ§ã«ãããç¹å®ã®å¤ã«å¯¾ããå¦çã®ã¿ãæ¼ãã¦ãã¾ããã¨ãé²ããã¨ãã§ãã¾ãã
æ¡ä»¶ä»ããã¿ã¼ã³ããã
ãã¿ã¼ã³ã«ã¯ãæ¡ä»¶ãä»ä¸ãããã¨ãå¯è½ã§ãã
ä¾ãã°ãæ£ã®æ°ãè² ã®æ°ã0ã§ããããå¦çãå¤ãããå ´åã
以ä¸ã®ããã« int
ã®æ°å¤ã®ç¯å²ãæã«æ¸ããã¨ã§ãå®ç¾å¯è½ã§ããã
let s = match n { 0 => "zero", 1..9223372036854775807 => "positive", _ => "negative" };
ãã¿ã¼ã³ã®å¾ã« if
ã使ã£ã¦æ¡ä»¶ãä»ä¸ããæ¹æ³ã§æ¸ããæ¹ãç´æçã«åãããããã¨æãã¾ãã
(ãã®ä¾ã ã¨ãmatch
ã§ã¯ãªããç´ ç´ã« if
å¼ã使ã£ã¦æ¸ããæ¹ãè¯ãæ°ããã¾ããã)
let s = match n { 0 => "zero", _ if n > 0 => "positive", _ => "negative" };
ãã®ãã¿ã¼ã³ãããæ©è½ã§ãããå¾ã§èª¬æãããè¤ååãã¨çµã¿åããããã¨ã§ãã®ç価ãçºæ®ãã¾ãã 詳ããã¯ããè¤ååãã®é ã§èª¬æãã¾ãã
ã«ã¼ã
while
C ã® while
æã¨åçã§ãã
let mut i = 100; while i > 0 { i -= 1; }
C ã® break
㯠break
, continue
㯠loop
ã§ãã
let mut i = 0; while i < 100 { i += 1; if f(i) == 0 { loop; } if g(i) > 0 { break; } }
loop
ç¡éã«ã¼ãã§ããwhile true {}
ã¨åãã§ãããå°ç¨ã®æ§æãç¨æããã¦ãã¾ãã
let mut x = 5; loop { x += x - 3; if x % 5 == 0 { break; } println(fmt!("%d", x)); }
for
é åçã®è¦ç´ ãã¤ãã¬ã¼ã·ã§ã³ãããã®ã§ãã å°æ¥çã«æå³ (æ§æ) ãå¤åããããªã®ã§ãããã§ã¯è¦ãç®ã®ç´¹ä»ã«çãã¦ããã¾ãã
rust 0.6 ã® for
for
ã¯ãå
é¨ã¤ãã¬ã¼ã¿ãå¼ã³åºãããã®æ§æã§ãã
let v = ~[1, 2, 3]; for v.each |x| { // å é¨ã¤ãã¬ã¼ã¿ each() ã®å¼åºã println(fmt!("%d", *x)); }
rust 0.7 ã® for
for
èªä½ã¯ 0.6 ã®é ããå¤ãã£ã¦ãã¾ããããã©ã¤ãã©ãªå´ãå
é¨ã¤ãã¬ã¼ã¿ããå¤é¨ã¤ãã¬ã¼ã¿ã¸ã¨ç§»è¡ãã¦ããããã
for
ãå©ç¨ããããã«ã¯å
é¨ã¤ãã¬ã¼ã¿ã¸ã®å¤æãå¿
è¦ã§ãã
let v = ~[1, 2, 3]; // iter() ã§ä½æããå¤é¨ã¤ãã¬ã¼ã¿ãã advance() ã§å é¨ã¤ãã¬ã¼ã¿ã«å¤æ for v.iter().advance |x| { println(fmt!("%d", *x)); }
(è¿ãï¼)å°æ¥ã®ãã¼ã¸ã§ã³
for
ãå¤é¨ã¤ãã¬ã¼ã¿ããµãã¼ãããããã«ãªããå
é¨ã¤ãã¬ã¼ã¿ã¯å»æ¢ããã¾ãã
// å¤é¨ã¤ãã¬ã¼ã¿ãç´æ¥å¼åºããIterable ãªåã®å ´åã iter() ã®å¼åºããçç¥ã§ããã for ~[1,2,3] |x| { println(fmt!("%d", *x)); }
[2013/9/22 追è¨]
rust 0.8 以éã® for
ã¯ä»¥ä¸ã®æ§æã¨ãªã£ã¦ãã¾ãã
for x in ~[1, 2, 3].iter() { println(fmt!("%d", *x)); }
æ°ãã for
æ§æã¯ãã¯ãã¼ã¸ã£ã§ã¯ãªãåç´ãªã«ã¼ãã®ã·ã³ã¿ãã¯ã¹ã·ã¥ã¬ã¼ãªã®ã§ãã¯ãã¼ã¸ã£ãæèããã以åã®æ§æã¨ã¯å
¨ãç°ãªã£ããã®ã«ãªã£ã¦ãã¾ãã
ãªããIterable
ãªåã«ã¤ãã¦ã¯ã0.8 æç¹ã§ã¯ãµãã¼ãããããæ示çã« iter()
ãæ¸ãã¦ã¤ãã¬ã¼ã¿ãåå¾ããå¿
è¦ãããã¾ãã
è¤åå
ã¿ãã«
é åã¨åãããã«ããã¤ãã®å¤ãã¾ã¨ãã¦ä¸ã¤ã®åã¨ãã¦æ±ããããã«ãããã®ã§ãã é åã¨ç°ãªããè¦ç´ æ°ã¯åºå®ã§ãããç°ãªãåã®å¤ãä¸ã¤ã®ã¿ãã«ã«å«ããã¨ãã§ãã¾ãã
let a = (1, "foo", true); // (int, &str, bool) å let b = (1, "bar", a); // (int, &str, (int, &str, bool)) å
n0
, n1
ãªã©ã®ã¡ã½ããã§ãn
çªç®ã®å¤ãå¾ããã¨ãã§ãã¾ãã
let tuple = (123, "a", true); tuple.n0() // 123 tuple.n1() // "a" tuple.n2() // true
æ§é ä½ (struct)
C ã®æ§é ä½ã¨ã»ã¼åãã§ãã
// Point æ§é ä½ã®å®ç¾© struct Point { x: float, y: float } // Point åã®å¤ãå®ç¾© let pt = Point { x: 1.0, y: 1.0 };
åå¼æ°ãã¨ãæ§é ä½ãå®ç¾©ãããã¨ãå¯è½ã§ãã
è¦ãç®ã¯ C++ ã® template
ã¨ä¼¼ã¦ãã¾ãã
struct Stack<T> { elements: ~[T] }
åæå (enum)
ååãã C ã® enum
ã¨åãã§ãããã©ã¡ããã¨ãã㨠union
ã®æ¹ãè¿ãã§ãã
é¢æ°åè¨èªã«è¦ªããã§ããæ¹ã«ã¯ãã代æ°çãã¼ã¿åãã¨è¨ãã°ããã³ã¨æ¥ãã¨æãã¾ãã
åæåã¯ãè¤æ°ç¨®é¡ã®å¤ããã¤åã§ãã
ä¾ãã°ã以ä¸ã® Shape
åã¯ã Circle
㨠Rectangle
ã¨ãã 2 種é¡ã®å¤ããã¡ã¾ãã
enum Shape { Circle(Point, float), // ä¸å¿ã®åº§æ¨ + åå¾ Rectangle(Point, Point) // å·¦ä¸ã®åº§æ¨ + å³ä¸ã®åº§æ¨ };
Circle
㨠Rectangle
ã¯åã Shape
åã®å¤ãªã®ã§ãã²ã¨ã¤ã®é
åã«æ ¼ç´ãããã¨ãã§ãã¾ãã
let shapes = ~[Circle(pt, 1.0), Rectangle(pt1, pt2)]; // Circle ã¨
ãã¡ãããC ã® enum
ã®ãããªä½¿ãæ¹ãã§ãã¾ãã
enum Direction { North, East, South, West } enum Color { Red = 0xff0000, Green = 0x00ff00, Blue = 0x0000ff }
æ§é ä½ã¨åæ§ãåå¼æ°ãã¨ãåæåãå®ç¾©ãããã¨ãã§ãã¾ãã
Rust ã®ããããã¨ããã§ä½¿ããã¦ãã Option<T>
å (Haskell ã® Maybe a
åã®ãããªãã®) ã¯ã
以ä¸ã®ããã«å®ç¾©ããã¦ãã¾ãã
enum Option<T> { Some(T), None }
è¤ååã¨ãã¿ã¼ã³ããã
ãã¿ã¼ã³ãããã«ã¯ãè¤ååãæ±ãä¸ã§é常ã«å½¹ã«ç«ã¤ åå² (destructuring) æ©è½ãããã³å¤æ°ã®æç¸æ©è½ãããã¾ãã
ä¾ãã°ã2 次å
ã®ã¿ãã«ã座æ¨ã¨è¦ãªãã¦ãåç¹ããã®è§åº¦ãåå¾ããé¢æ° angle
ã¯ã以ä¸ã®ããã«å®ç¾©ãããã¨ãã§ãã¾ãã
fn angle(vector: (float, float)) -> float { use std::float::consts::pi; match vector { (0f, y) if y < 0f => 1.5 * pi, (0f, y) => 0.5 * pi, (x, y) => float::atan(y / x) } }
ãã¿ã¼ã³ä¸ã«ç»å ´ããå¤æ°å (ä¸è¨ã§ã¯ x
, y
) ã¯ã_
ã¨åæ§ãä»»æã®å¤ã«ããããã対å¿ããå¤ãæç¸ããã¾ãã
ä¾ãã°ã(x, y)
ã¨ãããã¿ã¼ã³ããã£ãå ´åãx
ã«ã¯ã¿ãã«ã®1çªç®ã®è¦ç´ ã®å¤ããy
ã«ã¯2çªç®ã®è¦ç´ ã®å¤ãæç¸ããã¾ãã
ã¾ããå¤ã åå² ãã¦æç¸ããæ©è½ã¯ã let
ã§å¤æ°å®£è¨ããéã«ãå©ç¨å¯è½ã§ãã
let (a, b) = (123, 456); // ãã¿ã¼ã³ãããã§ã¿ãã«åã®å¤ããå¤æ° a, b ãçæ println(fmt!("a = %d, b = %d", a, b)); //=> a = 123, b = 456
æ§é ä½ã®ãã¿ã¼ã³ããã
æ§é ä½ã®ãã¿ã¼ã³ãããã¯ä»¥ä¸ã®ããã«æ¸ãã¾ãã
struct Point { x: float, y: float }; fn print_point(point: Point) { match point { Point { x: 0f, y: yy } => { println(fmt!("%f", yy)); } Point { x: xx, y: yy } => { println(fmt!("%f %f", xx, yy)); } } }
æ§é ä½ã®ãã£ã¼ã«ãåã¨åãååã®å¤æ°ã«å¤ãæç¸ããå ´åã以ä¸ã®ããã«å¤æ°åãçç¥ãã¦æ¸ããã¨ãå¯è½ã§ãã
fn print_point(point: Point) { match point { Point { x: 0f, y } => { println(fmt!("%f", y)); } Point { x, y } => { println(fmt!("%f %f", x, y)); } } }
åæåã®ãã¿ã¼ã³ããã
åæåã®ãã¿ã¼ã³ãããã¯ä»¥ä¸ã®ããã«æ¸ãã¾ãã
struct Point { x: float, y: float } enum Shape { Circle(Point, float), Rectangle(Point, Point) } fn area(sh: Shape) -> float { use std::float::consts::pi; match sh { Circle(_, size) => pi * size * size, Rectangle(Point {x, y}, Point {x: x2, y: y2}) => (x2 - x) * (y2 - y) } }
é¢æ°
é¢æ°ã¯ fn
ã§å®ç¾©ãã¾ãã2ã¤ã® int
åå¼æ°ãã¨ããæ»ãå¤ã¨ã㦠int
åã®å¤ãè¿ãé¢æ°ã¯ä»¥ä¸ã®ããã«æ¸ãã¾ãã
fn sum(a: int, b: int) -> int { return a + b; }
ãããã¯ã®æå¾ã® return
ã¯çç¥å¯è½ãªã®ã§ã以ä¸ã®ããã«ãæ¸ãã¾ã (æ«å°¾ã®ã»ãã³ãã³ãçç¥ããã¦ãããã¨ã«æ³¨æ)ã
fn sum(a: int, b: int) -> int { a + b }
Rust ã§ã¯ return
ãçç¥ããã¹ã¿ã¤ã«ãæ¨å¥¨ããã¦ãã¾ãã
ã¾ããæ»ãå¤ã ()
ã®å ´åã¯ã以ä¸ã®ããã«æ»ãå¤åã®æå®ãçç¥ãããã¨ãã§ãã¾ãã
fn void_function() -> () { return (); } fn void_function2() { }
å¼æ°ã¨ãã¦ä»»æã®åãã¨ããã¨ã®ã§ãããã¸ã§ããªãã¯ãªåã¯ä»¥ä¸ã®ããã«æ¸ããã¨ãã§ãã¾ãã
fn fun<T, U>(a: T, b: U) { // ä¸èº«ã¯çç¥ }
ã¯ãã¼ã¸ã£
ã¯ãã¼ã¸ã£ã使ã£ãã³ã¼ãã®ä¾ã§ãã
let mut n = 1; let plus_n = |x| n + x; // ã¯ãã¼ã¸ã£ãx ãå¼æ° plus_n(3); //=> 4 n = 3; plus_n(3); //=> 6 // åãæè¨ããå ´å let plus_n_2 = |x: int| -> int n + x; // è¤æ°ã®æãããªãã¯ãã¼ã¸ã£ let plus_n_3 = |x| { println(fmt!("%d", x)); x + 3 };
do
è¨æ³
do
è¨æ³ãç¨ããäºã§ãRuby ã®ãããã¯æ§æã®ãããªãã¨ãå¯è½ã«ãªãã¾ãã
fn each(v: &[int], op: &fn(v: &int)) { let mut i = 0; while i < v.len() { op(&v[i]); i += 1; } } // æ®éã«å¼ã³åºãå ´å each([1, 2, 3], |n| { println(fmt!("%d", *n)); }); // do è¨æ³ãç¨ããå ´å do each([1, 2, 3]) |n| { println(fmt!("%d", *n)); }
trait
/ impl
Rust ã¯ãtrait
㨠impl
ã¨ããä»çµã¿ã§ãªãã¸ã§ã¯ãæåãå®ç¾ãã¦ãã¾ãã
ã¯ã©ã¹ãã¼ã¹ããããã¿ã¤ããã¼ã¹ã®ãªãã¸ã§ã¯ãæåããããHaskell ã®åã¯ã©ã¹ã«è¿ãèãæ¹ã«ãªã£ã¦ãã¾ãã
// 表示å¯è½ã§ãããã¨ã示ã trait trait Print { fn print(&self); } // int åã« Print ãå®è£ impl Print for int { fn print(&self) { println(fmt!("%d", *self)); } } // ~str åã« Print ãå®è£ impl Print for ~str { fn print(&self) { println(*self); } }
以ä¸ã®ããã«ãã¦ä½¿ãã¾ãã
123.print(); // int åã® print ã¡ã½ãããå¼ã³åºã (~"123").print(); // ~str å ã® print ã¡ã½ãããå¼ã³åºã
(~[1, 2, 3]).print(); // ~[int] å㯠print ãå®è£ ãã¦ããªã
<anon>:38:0: 38:21 error: type `~[<VI2>]` does not implement any method in scope named `print` <anon>:38 (~[1, 2, 3]).print(); ^~~~~~~~~~~~~~~~~~~~~
éçãªã¡ã½ãããå®ç¾©ãããã¨ãå¯è½ã§ãã
self
å¼æ°ããªãã¡ã½ããã¯éçãªã¡ã½ããã¨ãªãã¾ãã
trait Shape { fn new(area: float) -> Self; } struct Circle { radius: float } struct Square { length: float } impl Shape for Circle { fn new(area: float) -> Circle { Circle { radius: sqrt(area / pi) } } } impl Shape for Square { fn new(area: float) -> Square { Square { length: sqrt(area) } } } let area = 42.5; let c: Circle = Shape::new(area); let s: Square = Shape::new(area);
åèæç®ã»é¢é£ãµã¤ãã¾ã¨ã
æ¥æ¬èªæç®ã¯å°ãªãã§ãããã
è¨èªã®èæ¯ãçµç·¯ã®ç´¹ä»
- Rust å ¬å¼ãµã¤ã (è±èª)
- Rust - Wikipedia (æ¥æ¬èª) ã¡ãã£ã¨æ å ±ãå¤ãã§ãããè¨èªã®æ¦è¦ãç°¡æ½ã«ã¾ã¨ã¾ã£ã¦ãã¾ã
- Rust - Mozilla ã®éçºããã·ã¹ãã ããã°ã©ãã³ã°è¨èª - ã«é¢ããã¤ã³ã¿ãã¥ã¼ - Graydon Hoare ããã¸ã®ã¤ã³ã¿ãã¥ã¼
è¨èªä»æ§ã«ã¤ãã¦
- å ¬å¼ã®ãã¥ã¼ããªã¢ã«
- Rust å ¬å¼ãã¥ã¼ããªã¢ã«ã®éå ¬å¼æ¥æ¬èªè¨³ (rust 0.4 ãã¼ã¹ãªã®ã§ãå°ãå 容ãå¤ãã§ã)
- Rust: A Friendly Introduction Rust ã®éçºè Tim Chevalier ã«ãããã¬ã¼ã³ãã¼ã·ã§ã³
- Rust for CXX programmers C++ ããã°ã©ãåãã®ç´¹ä»