å¹´æ«å¹´å§ Rust æ¯ãè¿ã
å¹´æ«å¹´å§ã®çãå¬ä¼ã¿ã¯ä½ããããã¨ããªãï¼ããããããã¨ã¯ãããã§ããããã§ããï¼ããã£ã¨ Rust ã®ã³ã³ãã¤ã«ã¨ã©ã¼ã¨è¦ã¤ãåã£ã¦ãã¾ãããå»å¹´ã¯å¾åããä¹ ã ã«ããããã®æéã Rust ã«è²»ãããã®ã§ãæã£ããã¨ãæ¯ãè¿ãããã¨æãã¾ãã
ææ権ã¨çåæéã¯ç解ã§ãã¦ãããæ¬çª
Rust ã¨è¨ãã°ææ権ã¨çåæéã§ããé£ããã¨è¨ãããçç±ã§ããé£ããã§ããé£ããã®ã§ããã¥ã¡ã³ãã Rust æ¬ã§ããã¼ã¸ãå²ãã¦è§£èª¬ããã¦ãã¾ãã Rust æ¬ãåºçããã¾ããããç解ãããããªã£ãã¨æãã¾ãã
ããææ権ã¨çåæéã¯ç解ã§ãã¦ãããæ¬çªã§ããããç¨åº¦ã®è¦æ¨¡ã®ã³ã¼ããæ¸ãã¦ééãã¦æ¸ãç´ãã¦ãç¹°ãè¿ããè¨è¨ã¨å®è£ ã®ãã¬ã¼ãã³ã°ãç©ã¾ãªãã¨èº«ã«ã¤ãã¾ãããã³ã³ãã¤ã«ã¨ã©ã¼ãåºããã³ãã¾ã ã¾ã ç解ãæµ ãã¨çæãã¾ãã GC ããªãã¨ã¯ãããããã¨ã§ãã
ã¨ã«ãããã¼ã¿åã®è¨è¨ã«è¦å´ãã
Rust ã§ã¯ãã¼ã¿åã®è¨è¨ã«ã¨ã«ããæ°ã使ããããå¾ã¾ãããé©å½ã«ãã£ããã³ã³ãã¤ã©ã«æ¢ãããã¾ããè¨è¨ã®éã¯æ¬¡ã®è¦ç´ ãèæ ®ããå¿ è¦ãããã¾ãã
- 循ç°åç §ã®æç¡ (Rust ã§ã¯ä¸æ¹éè¡ã®ãã¼ã¿åãæã¾ãã)
- å¯å¤æ§ (mut ã§æ¸ãã®ãæ¸ã¾ãªãã®ã)
- ææ権 (ã³ãã¼ããã®ãåç §ã使ãã®ã)
- çåæé (<âa> <âa> <âa> <âa> 以ä¸ç¥)
- ã¹ã¬ããã»ã¼ã
ç¥çµãããæ¸ãããªããã³ã³ãã¤ã«ã¨ã©ã¼ã¨æ¦ã£ã¦ããã£ã¨ãã¹ã¦ã®ã¨ã©ã¼ã解決ããã¨æã£ããæ°ããã¨ã©ã¼ãããããåºã¦ããè¾ã¿ãç§ã¯å®è£ ä¸ã®ã³ã¼ãã並è¡å¦çã«ãããã¨ããããã»ã¼ãã¹ã¦ã®ãã¼ã¿åãæ ¹æ¬çã«è¦ç´ãç¾½ç®ã«ãªãã¾ããã GC ããªãã¨ã¯ãããããã¨ã§ãã
é¢æ°ã®å¼æ°ã¯æèåæ¢ã§åç §æ¸¡ãã¨ãã¨ããããã«ããããªãã¦ããã£ã±ãçåæéã®ç解ãçãã¨ã©ããã§å¼ã£æãããã¾ããç§ã¯ Erlang VM ãå®è£ ãã¦ã¿ã¦ãããã§ããã Erlang ãªãã¸ã§ã¯ãã¯å¯å¤ãªãã¼ã¿ãããããªãã¸ã§ã¯ãéã§å ±æããããã¼ã¿ããããã©ããã¦ã Rust way ããå¤ããæ¹æ³ã§ç®¡çããªãã¨ããã¾ãããçµæãçåæéã Rust ã³ã³ãã¤ã©ãææ¡ã§ããªããªã£ãã®ã§ãåç §ã®æ±ãã§ããããã¨ã©ã¼ãåºãããã¦è¾ãã§ããå®è£ ã®é½åã§ææ権ã Rc/Arc (åç §ã«ã¦ã³ã) ã§ç®¡çãããã¨ãå¤ããã§ãããããããã¨åç §ãåãåã£ã¦ã Rc/Arc ã§ã©ãããç´ããªããã°ãªããªãå ´åãå¤ããã Rc/Arc ã ã¨æ¸¡ããã³ã«ã«ã¦ã³ãã®å¢æ¸ãçºçãããã Arc ã ã¨ã¢ãããã¯æä½ã®ã³ã¹ãããããããããªãã¨ããã©ã¼ãã³ã¹çã«ã©ããªã®ï¼ã¨ãèãå§ãã¦å ã å·¡ãã§ãã Rust 㨠VM ã¯è±èªã¨æ¥æ¬èªãããç¸æ§ãææªãªã®ã§ã¯ãªããã¨æããããå¾ãªãã
ãããç´°ãã API
Rust ã¯ãªã½ã¼ã¹ãå¶éãããç°å¢ or ãªã½ã¼ã¹ã管çãããç°å¢åãã®è¨èªãªã®ã§å½ç¶ã¨è¨ãã°å½ç¶ãªãã§ãããæ¨æºã® API ãç´°ããã§ããæååä¸ã¤ã¨ã£ã¦ãè¤æ°ã®åããããæåã³ã¼ãã®æ±ãããã£ã¦ç¸äºå¤æã«æéããããã¾ããä»ã® API ããåçãªã¡ã¢ãªç¢ºä¿ãé¿ããããã«ããããããã¨åãéããã¦ãããç解ããªãã¨å ã«é²ãã¾ããã GC ããªãã¨ã¯ãããããã¨ã§ãã
ãã¬ã¤ãã大éã«ããã¾ãã Rust ã§ã¯åºæ¬çãªæ©è½ã®ããªãã®é¨åããã¬ã¤ãã§è¡¨ç¾ããã¦ãã¦ãªã½ã¼ã¹ãå¶å¾¡å¯è½ãªãã§ãããé輪éããªãã«å¿ è¦ããªãã¨ãã¦ããã¤ãç解ããªãã¨å ã«é²ãã¾ããã GC ããªãã¨ã¯ãããããã¨ã§ãã
ãã£ã¨ãç§ã¯æ¨æº API ã«ãã¾ãæå¥ã¯ããã¾ãã (ä¸æºããªãã¨ã¯è¨ã£ã¦ãªã) ããã®ç´°ãããå¿ è¦ãªè¨èªã§ãããããã
unsafe ã®ã¸ã¬ã³ã
unsafe ã使ã£ã¦ãã¤ã³ã¿ãç´æ¥æä½ããã° 1 è¡ã§æ¸ãã§ãã¤é«éã«ãªãã®ã«ã¨æãå¦çã«ã¨ãã©ãåºãããã¾ãã大æµãããããå¦çã¯ææ権ãçåæéãå ±æãå é¨å¯å¤æ§ãéã¹ã¬ããã»ã¼ããªã©ã®åé¡ã§ããããããã§ãã Rust way ã«å¾ãã¨ä¸æ°ã«ã³ã¼ããè¨ãã¿ã¾ãããã¡ãããããªããªããããªè¨è¨ãããã®ããã¹ããªãã§ãããã¢ããªã±ã¼ã·ã§ã³ã®æ§è³ªã«ãã£ã¦ã¯ããããããªãã§ãããã GC ããªãã¨ã¯ãããããã¨ã§ãã
ãããã unsafe ã使ãã¨å½ç¶ãªããã¡ã¢ãªå®å ¨ã§ã¯ãªããªã£ã¦ãã¾ãã¾ããã¡ã¢ãªå®å ¨ã§ã¯ãªãç®æãä¸ã¤ã§ãããã°ãã¢ããªã±ã¼ã·ã§ã³ãã¹ã¦ãã¡ã¢ãªéå®å ¨ã§ãã Rust ã®é¢ç®ä¸¸æ½°ãã§ããã Rust ã¯èªåã®è¶³ãæã¦ãªããã¨è¨ããã¾ããã unsafe ã¨ä¸è¨æ¸ãã°ä»ããæã¦ã¾ããã§ãã®ã§ãå®å ¨ãªã¢ããªã±ã¼ã·ã§ã³ãä½ããããªãé ãæ»ã£ã¦ unsafe ãå ¨åã§åé¿ãã¹ãã§ãã大å¤ã§ãããã¸ã§ã GC ããªãã¨ã¯ãããããã¨ã§ãã
ã¡ãªã¿ã« VM ã¯ããããå¦çã®å¡ã§ãã Rust ã§å®è£ ä¸ã® Erlang VM ã¯ä»ã«ãããã¤ãããã¾ãããããã¨æ°è»½ã« unsafe ã使ããã¦ãã¾ããæ¬å®¶ VM ã®ç解ãå®è£ èªä½ãç®çã®ãããªã®ã§åé¡ã¯ãªãã¨æãã¾ããããã Rust ã®å¦ç¿ç®çã§ä½ãããä½ããªã unsafe ã使ã£ã¦ãã¾ãã¨ã¬ãã«ä¸ãã®å¦¨ãã«ãªãå¯è½æ§ãããããã§ãã GC ããªãã¨ã¯ãããããã¨ã§ãã
æå¤ã¨ä½¿ãéããªãï¼
Rust ã£ã¦ã使ãéãå¤ãããã§ãã¾ã使ãéããªãããããªããã¨æããã§ããã
ã¡ã¢ãªå®å ¨ã§ããã®ã¯ç¢ºãã«å¤§ããªã¡ãªããã§ãããå¶éããããªã½ã¼ã¹å ã§ããããããå¿ è¦æ§ãèãä»äºããã¦ãã大å¤æ°ã®äººï¼ã¤ã¾ã GC ã¤ãã®è¨èªã§æ®æ®µå°ã£ã¦ãªã人ï¼ã«ã¯ãå¦ç¿ãå«ããéçºã³ã¹ããèããã¨ãªã¿ã¼ã³ãå²ã«åããªãå ´åãå¤ãããããªããã¨æããã§ããã GC ã®åæ¢æéããªããã¡ã¢ãªå®å ¨ã§ããã¤é«éã¨ããç¹å¾´ãé åçã«æ ãã®ã¯ãããã¾ããã§ãããã 99% ã®æ¡ä»¶ã¯ Go ã§è§£æ±ºã§ããããªãã§ããç¡çã§ãããï¼
ãã¡ãããææã§é ¬ãã²ã£ã±ããã¦äººãé£ãã¦ãããä¼æ¥ãªãå¥ã«èªåãç¿çããªããã£ã¦ããã®ã§åé¡ã¯ããã¾ãããã§ãããããã¾ã§ç¿çã«é¢ãã¦ãã§ããããªããéã£ã人ã度éãªãã³ã³ãã¤ã«ã¨ã©ã¼åã³ unsafe ãåé¿ããããã®è¨è¨ã«ç²ããæ¿ã£ãç®ã§ unsafe ã¨æ¸ãã¦ãããããã¾ãããããã Option ã Result ã§ã¨ã©ã¼ãå¯è¦åãã¦ããã¨ã©ã¼å¦çãé¢å㧠foo().unwrap().bar().unwrap().baz().unwrap()⦠ã¨æ¸ãã¦ãããããã¾ãããã¨ãªãã¨ãæ¬å½ã«å®å ¨ãª Rust ã¢ããªã±ã¼ã·ã§ã³ãä½ããããªãã³ã¼ãã¬ãã¥ã¼ï¼çæéã§æ¸ã¾ãªãã¬ãã«ã®ï¼ãå¿ é ã«ãªãã¯ãã§ãã Rust ã¯ã¡ã¢ãªå®å ¨ãå®ç¾ãã¦ããããã¼ã«ã§ã¯ãªããããªããã¡ã¢ãªå®å ¨ãªã³ã¼ããæ¸ãããã©ãããæ¤è¨¼ãã¦ããããã¼ã«ã§ãã ä½åº¦æ¸ãç´ãã¦ãã¨ã©ã¼ãåºãã³ã³ãã¤ã©ã«æå¥ãè¨ã£ã¦ãä»æ¹ããã¾ãããä½åº¦ãéå®å ¨ãªã³ã¼ããæåºããããªãã«ã³ã³ãã¤ã©ãæå¥ãè¨ã£ã¦ããã§ãã
ææ権ã¨çåæéã®æ¦å¿µã«å¦ã³ãããããã ã¨æãã®ããããã¾ãã確ãã«ããã¨ã¯æãã¾ããã§ããªã¼ãããè¨ã£ã¡ããªãã ãã©ææ権ã¨çåæéã£ã¦ï¼å¤§å¤æ°ã®äººã«ã¯ï¼å¿ç¨ç¯å²ãããªãçãã®ã§ã¯ãªããã¨æãã¾ããæ®æ®µ GC ã¤ãã®è¨èªã使ã£ã¦ãã¦ãã¡ã¢ãªã«ã¤ãã¦æèãããã¨ãªãããåå¼·ãããã¨ãããªããã使ãã®è¨èªã® GC ã®ã¢ã«ã´ãªãºã ã«ã¤ãã¦å¦ãã ã»ããå½¹ç«ã¤ã ãããç¥èã®åºä¸ãã«ããªãããããªããã¨æããã§ããã
ã ã£ã¦ãæéã¯æéã ã¨æããã§ãããä»äºã§ Rust ã使ããã¨ã«ãªã£ãã¨ãã次ã®ä»äºã®æåãªåè£ã ã¨ããå¦ç¿ã®å¿ è¦æ§ããã人以å¤ã¯ããã®æéãä»ã®è¨èªãæè¡ã®å¦ç¿ã«ä½¿ãã»ããææ義ã ã¨æããã§ãããææ権ã¨çåæéã®èãæ¹ã身ã«ã¤ãããªãç¸å½ãªæéã®æè³ãå¿ è¦ã§ããã ã£ãããã®æéãè±èªã Go ã®å¦ç¿ã«ã§ãå½ã¦ãã»ããããã§ããçé¢ç®ã«ãå¼·ãåä»ãã¨é«éé¢æ°ãå¦ã³ãããªã OCaml ãããã§ããã Real World OCaml ã« GC ã®è§£èª¬ãããã¾ãã Rust ããããã OCaml ãã¹ã¿ã¼ããã¦ãã OCaml ããããã Rust ãã¹ã¿ã¼ã¯ãã¾ããããã¶ãã
ãªãã§ããæ²è¦³çå¦å®çãªãã¨ã°ããæ¸ãã®ãã¨è¨ã㨠(ç§ã®æ¸ãè¨äºã¯ãããªãã°ã£ããã§ããâ¦) ãé¢æ°åè¨èªã¯é£ããã¨æ¬é ããã®ã«åãç¹å¾´ãæ㤠Rust ã«ã¯å¥½å°è±¡ãæã£ãããéçºçµé¨ãªãã« Rust ã«æªæ¥ã確信ãã人ãå¤ãè¦åããããæããããããã§ããããå¥ã«ã©ãæããã¨èªç±ã§ããï¼ ãã ãå人çã«è¦ããèããããããã¡ã«ãã¡ãã£ã¨ä¸è¨è¨ããããªã£ãã®ã§ãã