第äºããã°ã©ãã³ã°è¨èªã¨ã㦠Rust ã¯ãªã¹ã¹ã¡ãã¾ãã Nim ãããã®ã§ã
ãããã Rust 触ã£ã¦ã¿ããããªãã¨æã£ã¦ããæ¹ãããã¦ããã¾ããããããã°ã©ãã³ã°ãå«ãã«ãªãã¾ããã
Nim ãããã¾ãããã
ãã§ã« Rust ã«æãã§å¿ã¶ã¡æããæ¹ã
Nim ããã£ã¦å¿ã®å·ãçããã¾ãããã
Rust ããã£ã¹ãããï¼å°çªãªï¼ã¨æã£ã¦ããæ¹ã
Nim ãããã®ã§ã(天ã«è½ãåé¿é³)ã
Nim ã®é åã Rust ã¨æ¯è¼ãã¤ã¤çæ§ã¸ãä¼ããã¾ããã(両æãåºãã)ã
ãããã¿ããªãRust ãªãã¦æ¨ã¦ã¦ãã£ã±ã人æ°ãç¡ã Nim ãããã®ã§ã(ããã¡è²ã«è¼ã)ã
è¡æ«ã®ã»ãã³ãã³ãå¿ è¦ãªã
ã¿ã¤ãæ°ãããããæ¸ãã¾ãã
Rust ã«ã¯ãã¡ããå¿ è¦ã§ãã
main ãè¦ããªã
ã¹ã¯ãªããè¨èªæè¦ã§ãããªãã³ã¼ããæ¸ãã¾ãã
Rust 㯠main ãå¿ è¦ã§ãã
æ¨æºåºåã¸ã®æåååºåã楽
Nim ã§ã¯ echo ã§æ¹è¡ä»ãã®åºåãã§ãã¾ããshell ã¨åãã§ãããé常ã¯æ¹è¡ä»ãã§åºåãããã¨ã®æ¹ãå¤ãã§ããã
Nim ã¯ããã£ã¡ã
ããããã¨ã¯ç°¡åã«ã§ããããã«ãªã£ã¦ãã¾ãã
ãã㪠Nim ã® echo ã¯å¯å¤å¼æ°ã§å¤ãåãåãåãä½ãªãã ããã¨ãæ§ããªãã«åºåã§ãã¾ãã
let n = 10 let str = "HOGE" echo "Number: ", n, " String: ", str
ä¸æ¹ Rust ã¯
let n = 10; let str = "HOGE"; println!("Number: {} String: {}", n, str);
ãªããããå¤ãããã¯ãã§ãã¡ãã¡ã³ã£ãããããªãããããªãã§ãããã使ããã®ãåé·ã ã¨ã²ã³ããªãã¾ãã
å¤æ°ãç´æ¥ã¶ã¡è¾¼ããã¨ãåºæ¥ã¾ããããã
let n = 10 echo n
æ®éåºæ¥ãã§ãããããªããã»ã»ã»ãã¨ããã Rust ã¯åºæ¥ãªãã
let n = 10; println!(n); <- ã¨ã©ã¼ println!("{}", n); <- æ¯åº¦ãããæ¸ãããã
ãããã£ãã
C ã®é¢æ°ãæ°è»½ã«æã£ã¦ãã
ãã£ãä¸è¡è¶³ãã ã㧠C ã®é¢æ°ã使ããã¨ãåºæ¥ãããã«ãªãã¾ãã
proc printf*(format: cstring) {.header: "<stdio.h>", importc: "printf", varargs.} let n = 10 let str = "HOGE" printf "Number: %d String: %s\n", n, str
ã©ãã§ãããï¼C ã®è³ç£ãæ°è»½ã«ä½¿ããã¨ãã§ãããã§ããSWIG çã®é¬±é¶ããã©ããã¼ã使ããã¨ç¡ãã§ãã
Rust ã®å ´åã¯ãå¤åã«ãããã©ããã¼è¡ãã§è¶
絶é¢åãããã§ããæ¯è¼ç¨ã«æ¸ããããã§ããçµæ§ãªæç« éã«ãªãã®ã§ããã¾ãã
mut mut ããªãã¦è¯ã
Rust ã¯ã¾ã¨ããªå¤æ°ã使ããã¨ãã㨠mut mut ããªãã¨ããã¾ãããããã©ã«ãã ã¨å代å
¥ã§ãã¾ããããã
æ®éå代å
¥ãã¾ããã¾ãããï¼å®æ°ã©ã¤ã¯ã«ä½¿ãããæ©ä¼ãªãã¦æ®ã©ãªãã§ãããï¼ãªã®ã« mut ãæ¯åº¦æ¸ãããã¾ãã
let n:int = 10 let mut m: int = 10
Nim ãªãããã§ããã
let n = 10 # immutable var m = 10 # mutable
ç´ æµã
ææè ã»åç¨ãªãã¦ããã§ã¤ã©ã¤ã©ããªã
ãRust ã«ã¯ C ã®ãã¤ã³ã¿ãå¯æãè¦ããã¬ãã«ã§é«ããã³ãç«ã¤éå£ã®åå¿è
ã¬ã¼ããæªå¤¢ã®"ææè
ã»åç¨"ã®æ¦å¿µãåå¨ãã¾ãã
ããã°ã©ã ã«æ
£ãã人éã§ããæ··ä¹±ã«é¥ããæ¸ãã¦ããæä¸ã«ç²¾ç¥åã¨äººçã®è²´éãªæéãã¬ã³ã¬ã³åã£ã¦ããã究極ã®å«ãããã§ãã
Rust ã¯å¤æ°ã®ã³ãã¼ãã¡ããã¨å ã®ãã¤ã使ããªããªãã¯ã½ä»æ§ãªã®ã§ããæ¸ãæã«ã¡ãªãããªãã¦ä¸åç¡ããC++ã®æªããã¡ã¢ãªç®¡çã®åªããæã¡è¾¼ãã§æ¥ã¦ãã®ä¸ã§ãããã«æªãé¨åãããã©ã«ãã«ããæãã§ãã
struct Point { x: i32, y: i32, } fn Print(p: Point) { println!("x = {}, y = {}", p.x, p.y); } fn main() { let mut a: Point = Point{ x: 10, y: 15 }; Print(a); // ã¨ã©ã¼ï¼ println!("x = {}, y = {}", a.x, a.y); }
Print(a) ã§1åã³ãã¼ããã¦ããã®ã§ãã®å¾ä½¿ãã¨æ»ã«ã¾ããã¦ã½ã§ããï¼ã¨æãã§ããï¼ãã³ãã§ãã
ããã¦ããªããã£ãåãªãOKã¨è¨ã Java ã«ä¼¼ãããã¹ã¿ã®åªãããªãã±ã§ä»ãã¦ã¾ãã
ããããã¾ã§é¢æ°ã¯ã»ã¼å
¨ã¦æ示çã«åç
§æ¸¡ãããããã¡ã«ãªãã¾ãã
ãã ã£ããããã©ã«ããããã¨ããï¼ & ãã¤ãã¤ãæ¸ããããªãã¯ã¬ï¼ãã¨æããªãã®ã£ã¦ãã種ã®æè½ã ã¨æãã¾ãã
struct Point { x: i32, y: i32, } fn Print(p: &Point) { println!("x = {}, y = {}", p.x, p.y); } fn main() { let mut a: Point = Point{ x: 10, y: 15 }; Print(&a); println!("x = {}, y = {}", a.x, a.y); }
ããã ã¨ã¾ãã¨ã©ã¼ã«ã¯ãªãã¾ãããããåç §ã ããã¨ãã£ã¦ãããªãã¨ãã£ããæ»ã«ã¾ãã
fn Print(p: &Point) { println!("x = {}, y = {}", p.x, p.y); p.x = 10; <- die }
ã¤ãã¥ã¼ã¿ãã«ã ããã§ãã£ã¦ãã¯ãã»ã»ã»ã
ã ããããæ¸ãã§ãã£ã¦ã
fn Print(p: &mut Point) { println!("x = {}, y = {}", p.x, p.y); p.x = 100; } fn main() { let mut a: Point = Point{ x: 10, y: 15 }; Print(&mut a); println!("x = {}, y = {}", a.x, a.y); }
ã¯ãæ¥ããmut mut mut mut mut mut mut mut mut ãããããããï½ï½ï½ï¼ï¼ï¼
ãªãã§ãã使ãæ¹ãé¢åèããããããã§ããããã®è¨èªãä½ã£ã¦ããæ¹ã ã¯ã
ãã®ä»å貸ãã®åªããããªã«ããè¶ çã沢山ã§ããããããç§ã¨ã¯ã»ã³ã¹ãå ¨ãåããªãã§ãã
ã¬ã ã ãã ãããããããããããããï¼ï¼ï¼ï¼ï¼Rustãï¼ãããåã«ã¯é ¼ã¾ãï¼malloc 㨠free ã俺ã«ããããããã ããï¼ï¼ãããªè¨³ã®ãããããã®ã«ä»ãåãããããã ã£ããèªåã§ã¡ã¢ãªç®¡çããæ¹ããã·ã ãï¼ï¼ï¼
ã¨ããã¿ããªçºçãã¾ãããããææ ¢å¼·ãã§ããã馬鹿ãããªãã®ã
ã¨ã£ã¦ãè¯ãåã§ãã Nim ã«ã¯ãããªåªããã"ã¯ã±"ããªãã§ãã
type Point = object x: int y: int proc print(this: Point) = echo "x = ", this.x, ", y = ", this.y var p = Point(x: 10, y: 15) p.print() echo "x = ", p.x, ", y = ", p.y
ã¾ãæ®éã¯ããã§ãããã»ã»ã»ãRust ãã¶ã£é£ãã§ç°å¸¸ãªã ãã§ããããããªãã§ãã
ã¡ãªã¿ã« Nim ã®å ´å print(p) 㨠p.print() ã¯æ¸ãæ¹ãéãã ãã§æå³ã¯åãã§ãã
åç §ã§æ¸¡ãå ´åã¯ãããªãã¾ãã
type Point = object x: int y: int proc print(this: ref Point) = echo "x = ", this.x, ", y = ", this.y this.x = 100 var p = Point.new p.x = 10 p.y = 15 p.print() echo "x = ", p.x, ", y = ", p.y
new 㧠Point object ãä½æããã¨åç §ã®ãªãã¸ã§ã¯ããåºæ¥ã¾ããããã渡ãããã« print å´ã®å¼æ°ã«ã¯ ref ãã¤ãã¦ããã¾ããnew é¢æ°ã§ã¡ã³ãã«å¤ãå²ãå½ã¦ããã¨ã¯åºæ¥ãªãã®ã§å¾ãã渡ãã¦ããã¾ãã
ã¤ã£ã¦ãä¸ã®ãã¤ã¯ããã¾ã§ Rust ã¨ä¼¼ãã¦æ¸ããããããªããã£ã¦è©±ã§ãããªæ¸ãæ¹ãã¾ããã
æ®éãªãã¸ã§ã¯ããªãã¦åç
§ã ããã£ã¦äºã§ Nim ã§ã¯ä»¥ä¸ã®ããã«æ¸ãã®ãæ
£ä¾åãã¦ãã¾ãã
type Point = ref PointObj PointObj = object x: int y: int proc print(this: Point) = echo "x = ", this.x, ", y = ", this.y this.x = 100 var p = Point(x: 10, y: 15) p.print() echo "x = ", p.x, ", y = ", p.y
ãªãã¸ã§ã¯ãã¨ãã®ãªãã¡ã¬ã³ã¹ãåæã«å®ç¾©ãã¦ãé常使ããªãæ¹ã®ãªãã¸ã§ã¯ãå´ã«ãµãã£ãã¯ã¹ãã¤ãã¦ããã¨ãã¾ãç´ ã®ãªãã¸ã§ã¯ããä½ããããä½ããããã£ã¦è©±ã§ãã
èªåã¯æ£ç´ãªãã¡ã¬ã³ã¹ã ãã§è¯ãã®ã§æ´ã«æãæãã¦ããæ¸ãã¾ããã©ãã
type Point = ref object x: int y: int
ãã¿ã¼ã³ãããï¼case ã§ããï¼
Nim ã case ã§ããã£ã½ãæ¸ãã¾ãã
è¤å¼ãã¿ã¼ã³
fn main() { let x = 1; match x { 1 | 2 => println!("1 | 2"), 3 => println!("3"), _ => println!("other"), } }
let x = 1 case x of 1, 2: echo "1 | 2" of 3: echo "3" else: echo "other"
ç¯å²
fn main() { let x = 1; match x { 1...5 => println!("1...5"), _ => println!("other"), }; }
let x = 1 case x of 1..5: echo "1..5" else: echo "other"
case ã®è¿ããåãåã
fn main() { let x = 1; let s = match x { 1 => "one", 2 => "two", _ => "other", }; println!("{}", s) }
let x = 1 let s = case x of 1: "one" of 2: "two" else: "other" echo s
åé æç¸
Nim ã¯æ¨æºã§ã¯ã§ãã¾ããã
https://github.com/andreaferretti/patty
ãçªã£è¾¼ããã¨ã§å¯è½ã§ãã
ä»æ§ãã°ããªã
Rust ã®ä»¥ä¸ã®æåã¯å ¨ãç解ãã§ãã¾ããã
fn main() { let x = 'x'; let c = 'c'; match c { // x: c c: c x => println!("x: {} c: {}", x, c), } // x: x println!("x: {}", x) }
æ®é x ã«ãããããã¨æããªãã§ããããã
ããã«ãã®ç´å¾ x ã 'c' ã«å¤ãã£ã¦ãã¨ãäºæ³ã ã«ãã¾ãããã
ã¾ãæ®éã¯ãããªæ¸ãæ¹ããªãã¨æãã¾ãããããªèª¿åã§ã¯ã©ãã§ã©ãããããäºæ¸¬ä¸å¯è½ã§ãæããããã¾ãã
Nim ã¯ãããªæ¸ãæ¹ããããåºæ¥ã¾ããã
ã³ã³ãã¤ã©ãã±ãããããªã
nim c -r hoge
ãã㧠hoge.nim ãã³ã³ãã¤ã«ãã¾ãã
æ¡å¼µåãªãã¦æå®ããå¿
è¦ããã¾ããã
-r ã§å®è¡ããã¾ãã
Rust ã®å ´å
rustc hoge <- ãã¡
ã³ã³ãã¤ã«ã¨åæã«å®è¡ãããã¨æã£ãã
rustc hoge.rs && ./hoge
ãã¼ãã»ã»ã»
å®è¡é度ã»ã¡ã¢ãªä½¿ç¨éã»ãã¡ã¤ã«ãµã¤ãºãå°ãã
Rust ã¨æ¯ã¹ã¦ Nim ã®å®å¹é度ã¯ã©ã£ããããããéãã§ãã
Rust ã¯ããã ãã¤ã©ã¤ã©ããæ¸ãæ¹ãå¼·å¶ãããã«ãé¢ããããããã¦éããªãã¨ãããåãããã¾ãã
ã³ã³ãã¤ã«å¾ã®ãã¡ã¤ã«ãµã¤ãºã¯è©±ã«ãªããªãã¬ãã«ã§æ¯ã¹ç©ã«ãªãã¾ããã
fizzbuzz ã®ä¾(FizzBuzz を無駄にベンチマークしてみた By Nim、golang、Rust、Crystal、その他 - 強まっていこう)ã§è¨ãã¨
é ç® | Nim | Rust |
---|---|---|
å®è¡é度 | 0.37s | 0.44s |
ãã¡ã¤ã«ãµã¤ãº | 82K | 3.4M |
ã¡ã¢ãª | 356K | 900K |
ãããªæãã§ãã
ãã¯ãã®ã·ã³ã¿ãã¯ã¹ãå¥ã§è¦ããå¿ è¦ããªã
Rust ã®ãã¯ãã¯æ§æãå
¨ãå¤ãã£ã¦ãã¾ãã¾ããããã¦ããã¯è³ãå
¨åã§åä»ãæå¦ããç´ æµãªä»ä¸ããã¨ãªã£ã¦ããã¾ãã
å
¬å¼ã§èªãããã¯ããã¼ã¹ã®ã³ã¼ãã®æ¬ ç¹ã¯ãçµã¿è¾¼ã¿ã«ã¼ã«ã®å°ãªãã«ç±æ¥ãããã®ã³ã¼ãã®ç解ã®ãã¥ããã§ãããã¨è¨ãã®ãã¡ãã代ç©ã§ããªãããããããã¨è¨ãè¨èããåºã¾ããã
Nim ã¯æ§æããã®ã¾ã¾ä½¿ãã¾ãããªã®ã§å¼·åãªãã¯ãã使ãããªãããã®éå£ã®ä½ãã¯æ¯ã¹ç©ã«ãªãã¾ããã
Rust ã®è¯ãã¨ãã
ãããã« Rust ãæ¹å¤ã°ãããã¦ãã¦ã¯å ¬å¹³æ§ã«æ¬ ããå ±éã¨ãªãå®é¸ããæããã¾ããRust ã«ãè¯ãã¨ããã¯ããã¾ãã
fn <- çãï¼
proc <- é·ãï¼
ããã¯ã¡ãªããã§ãããã¿ã¤ãã2åãã¤æ¸ãã®ã¯ç´ æµãªãã¨ã§ãããã£ããããã ã馬鹿ããåé·ããæ¼ãä»ãã¦ããè¨èªã®ãããã¦ãä½æ ããã ããã£ãããã¦ããã®ãããã£ã±ãæå³ãããããªãã§ããããçµå±ãã£ã¹ã£ã¦ãããã»ã»ã»ã