Rust ã¯ä½ãæ°ãããªãã®ã
disclaimer
追è¨ã§æ³¨ææ¸ã足ãã®ã¯ã©ããªãã§ãããããã¨ãããã¨ã§è¿½è¨ã§ãã
- å¥ã«ã®ã§ã ã§ã¬ã³ã¬ã³æ¸ãã¦ãã¨ãã§ã¯ãªãã§ãã
- ããã³ã³ã®æ¹ãå«ãã¦ããã¥ã¡ã³ãå ¨é¨èªãã ãAPI ä¸éãçºãããåæ¨è«ã¨ããããªã¼ã¸ã§ã³æ¨è«é¨åã®æ¦è¦çºãããé¢é£ããè«æèªãã ãè¨èªè§¦ã£ããç¨åº¦ã®äººéãæ¸ãã¦ã¾ãã
- æ£ãããªããä¸æ£ç¢ºãªé¨åãããã¨æãã¾ãã
- éã§ããããã¾ããã
TL;DR çã«ã¯ãC++ ç½®ãæããããã®è¨èªã¨ãã¦ä½ãããã®ã§å½ç¶ C++ ã«ãã£ãæ¦å¿µå¼ãç¶ãã§ã¾ããã¿ãããªè©±ã§ãã
以ä¸è¿½è¨åã®å ã®å ¨æã
以ä¸ã®è¨äºãçµæ§äººæ°ã¨èãã¾ããã
Rustは何が新しいのか(基本的な言語機能の紹介) - いもす研 (imos laboratory)
ããã§ã¯ãè¨äºä¸ã®ãæ°ãããªããé¨åã historical ãªè©±ã交ãã¦èª¬æããå½¢ã§ä½ãæ¸ãããã¨æãã¾ãã
è¨äºãå¦å®ãããããªãã®ã§ã¯ãªãã§ãã寧ãããã¾ã¨ã¾ã£ã¦ããã¨æãã®ã§ããã綺éºã«ã¾ã¨ã¾ãããã¦ãã¦ãåãã£ã¦ãã人ã«ããåãããªããé¨åãããããªãã¨æããçãï¼ãã¼ãã¼ãã¼ã§ããã©ï¼åã£ã次第ã§ãã対象èªè 㯠GC ã®ããè¨èªã§ããã³ã¼ããæ¸ãããã¨ãç¡ã人ããªã®ããªã
ãRust ã®ããã°ã©ãã³ã°è¨èªã¨ãã¦ã®ç«ã¡ä½ç½®ãè£è¶³
å è¨äºã§ã¯ãè¨èªã®ä»ã¨æªæ¥ã«ã¤ãã¦æ¸ããã¦ãã¾ãããããã§ã¯æ話ããã¾ãããã
Mozilla ã¯æ§ã ãªãããã¯ããéå» C++ ã§å®è£ ãã¦ãã¾ããã巨大ãªãããã¯ããå¤ããéçºã«ã¯èªä½ã®éç解æãã¼ã«ãå©ç¨ããã¦ãã¾ããã
ããããåé¡ãèµ·ãã¾ããéç解æãã¼ã«ãå©ç¨ãã¦ãã C++ ããã¼ã¹ããã©ã¤ãã©ãªããã¼ã¸ã§ã³ã¢ããããããC++11 辺ããã使ããªããªã£ã¦ãã¾ãã¾ãããããã以åããä½è ã¯ããã¡ã³ããã³ã¹ãã¦ããããMozilla ã確ãã¡ã³ããã³ã¹ããã¦ãããã ã£ãã£ãâ¦ããåããã
Mozillaï¼ã®éçºãã¼ã ã®ä¸é¨ï¼ã¯ãè¨èªå¦çç³»ã®ãã©ã°ã¤ã³ã¨ãã¦éç解æãã¼ã«ãå®è£ ã§ããªãããã¨èãã¾ãããçµæãçã¾ããã®ã gcc compiler plugin ã¨ã³ã³ãã¤ã©ãã©ã°ã¤ã³ Dehydra ã§ããããgcc ã® ASTï¼æ½è±¡æ§ææ¨ï¼ã¯ããã¼ã«ãä½ãã«ã¯ä½ç½®æ å ±çããã¾ãã«ãä¸è¶³ãã¦ãã¾ãããå°ã話ãããã¾ãã diagnostic ã大åãªãã®ã¨ãã¦è¨è¨ããã clang ã® AST ã¯ããã®è¾ºãããªãé æ ®ã®ããè¨è¨ãããã¦ãã¾ããã¾ããgcc ã® mangling ã®ã¿ã¤ãã³ã°ãå®å®ãã¦ããããã³ã³ãã¤ã©ãã©ã°ã¤ã³ã«ããã£ã¦ããã¿ã¤ãã³ã°ã§ä½æ ãã·ã³ãã«ã mangling ããã¦ãããããªãã£ãããã¨ãã£ããã°ãããã¾ãããããããã³ã³ãã¤ã©ãã©ã°ã¤ã³ãªãã¦æ³å®ããéçºããã¦ããã®ã§ãã°ã¨ããªããªãè¨ãã¥ããã®ã§ããã
èªåãè¦ã¦ããç¯å²ã§ã¯ãä¸è¿°ã®ãããªãã¨ã Mozilla ã¯è¡ã£ã¦ãã¾ãããclang ãã³ã³ãã¤ã©ãã©ã°ã¤ã³ã®ä»çµã¿ãæã£ã¦ããã®ã§ãããMozilla ã¯ã代æ¿è¨èªãä½ããéãé¸ã³ã¾ããããã¯å¿è« C++ ãæ¨ã¦ããã¨ãæå³ãã¾ãã
çµæã¨ãã¦èªçããã®ã Rust ã§ãï¼Cyclone ã®è©±ã¯å²æãã¾ãï¼ã大è¦æ¨¡ C++ ãããã¯ãã®ã³ã¼ããç½®ãæãããããããªè¨èªã§ãªããã°ããã¾ããã§ãããããã¦ãRust ã¯ååã«ãããæããã¦ããããã«æãã¾ãã
ãã¹ãã¼ããã¤ã³ã¿ãçã¾ããã¾ã§ãè£è¶³
C++ ã«ã¯ GC ãåå¨ãã¾ãããå°ãªãã¨ãä»ã¯ãçã®ã¢ãã¬ã¹å¤ããã¤ã³ã¿åã使ã£ã¦æ±ããã¨ãã§ãã¾ããéã«ããã¨ãGC ããã£ã¦ããããã´ãæ¨ã¦ããï¼GC ã«ã¯ compaction çã®ä»äºãããã¾ããå²æï¼èªåã§ããªããã°ãªããªããã¨ãããã¨ã§ããGC ã®ãªãè¨èªãå©ç¨ãããã¨ããªã人ã«ã¯æ³åããªããªãã¤ããªãã¨æãã®ã§ããã常ã«ãªã½ã¼ã¹ã®ç®¡çãããªããæ¬æ¥ã®å¦çãè¨è¿°ããªããã°ãããªããã¨ããã®ã¯ããªã大å¤ã§ãã
大å¤ã§ãããããã®ä»äºã楽ã«ãããã¨ä»çµã¿ãä½ããã¾ããå è¨äºã§è¨åããã¦ãã unique_ptr 以åãããC++ ã«ã¯ã¹ãã¼ããã¤ã³ã¿ãåå¨ãã¾ãããæ§ã ãªã©ã¤ãã©ãªãç¬èªã«è²ã ä½ã£ãããã¦ãã¾ããããæ¨æºã©ã¤ãã©ãªãã㯠auto_ptr ãæä¾ããã¦ãã¾ãããããã¯ãRust ã«åããï¼Copy trait ã®è©±ã¯å²æï¼ä»£å ¥æ¼ç®åã move ã¨ãªããããªãã¤ã³ã¿ãã®ããã«æ¯èãã¯ã©ã¹ã§ãããããããä»£å ¥æ¼ç®åãé常ã¯ã³ãã¼ã¨ãã¦ä½¿ããã¦ããå½æãauto_ptr ã® semantics ã«ã¯å¤ãã®éçºè ãæ··ä¹±ããããã¾ãããçµæã¨ã㦠auto_ptr ã¯ã使ããªãã¨è¨ããã¦ãã¾ãã¾ããã使ããªã¨è¨ããã¦ã¯ãã¾ãã¾ããããã©ããã®é ãã move semantics ã¯ï¼æ¨æºã©ã¤ãã©ãªã¬ãã«ã§ï¼åå¨ããããã§ããã
è¨èªãµãã¼ããå ¥ã£ãã®ã¯ãå è¨äºã«ããéã C++11 ã§ããããã以åãã Boost ã©ã¤ãã©ãªçã§ãã¹ãã¼ããã¤ã³ã¿ãã¹ã¬ããï¼ã¹ã¬ããã¯ææè ãä¸äººã§ãªããã°ãªããªããªã½ã¼ã¹ãªã®ã§ãmove ãå¿ è¦ã§ãï¼ã«å¯¾ãã move ã®ãµãã¼ãã¯ããã¾ãããã¾ããlifetimeï¼å¯¿å½ï¼ã®æ¦å¿µã«ã¤ãã¦ã Boost scoped_ptr ã shared_ptr ã®é ããã©ã¤ãã©ãªã¬ãã«ã§ç¤ºããã¦ãã¾ãããèªç¥åº¦ã¯â¦å ãè§â¦
ãRust ã®è¨èªæ©è½ãè£è¶³
ã©ã®æ©è½ããä½ãããã®æ¢åã®è¨èªã«åå¨ãããã®ã§ãããC++ ã«ã¯ãªãã¦è¾ãã£ããã¿ãããªæ©è½ã§ãããä¸å¿æ¸ãã¦ããã¨ãgenerics 㨠template ã¯ä¼¼ã¦ããããã§å ¨ãç°ãªãè¨èªæ©è½ã§ãã
çµå±ä½ãæ°ãããªãã®ã
lifetime ã move semantics 㯠C++ ã«å ããåå¨ããæ¦å¿µã§ããããããè¨èªã®ãµãã¼ãã®ãããªãã«é¢ããããraw pointer ãæ±ãè¨èªã§ããã°ãæèããªãã¨ãããªããã®ã§ããçããã C ã§ãããªã³ã¼ããæ¸ããçµé¨ãããã®ã§ã¯ãªãã§ããããã
struct string { char *ptr; void (*free_fn)(char *ptr); }; void free_string(char* ptr) { free(ptr); } // malloc ãªã©ã§çæããã char* ã®ããã®éæ¾é¢æ°ã void do_nothing(char*) { ; } // ãªãã©ã«ãªã©ã§çæããã寿å½ãæããªãããããã¯å¯¿å½ãã¹ã¿ãã¯ã«ç®¡çããã¦ãã char* ã®ããã®ä½ãããªã解æ¾é¢æ°ã
ãã¹ãã©ã¯ã¿ã«ããèªå解æ¾ãããªããã®ã®ãããã¯ã¾ããã C++ ã®ã¹ãã¼ããã¤ã³ã¿ã«ããã custom deleter ã§ãããçµå±è¨èªãµãã¼ããããããªããã ãã§ãC ã ããã C++ ã ãããåããã¨ãããããã§ããã¡ãªã¿ã«ããã®ãããªè¤æ°ã® lifetime ãåæã«æ±ãä»çµã¿ã¯ãRust ãã¡ããã¨ãµãã¼ããã¦ãã¾ããCow trait ã ã£ããéã£ããããã¾ããã
é·ããªãã¾ããããã¤ã¾ãæ°ãããªãã®ã¯ãlifetime ã move semantics ã®æ¦å¿µã§ããC++11 以éãå ããã¨ãmove semantics ã®è¨èªãµãã¼ããæ°ãããã®ã§ã¯ããã¾ããã
çµå±ä½ãæ°ããã®ã
æ¶å»æ³ã§æ®ã£ãã®åºãã ãã§ããæ°ããã®ã¯ lifetime 㨠linearity ã®è¨èªãµãã¼ãã§ãã
Rust ã§ã¯å¤æ°ã®å¯¿å½ãã³ã³ãã¤ã©ããã§ãã¯ãã¦ããã¾ããããã¯åç §ãåãã§ããC++ ã®åç §ã¯ããç¨ã«ããã®åç §è§¦ã£ã¦ãéã«æ»ãã ãããªãããªâ¦ãã¨å¿é ã«ãªããã¨ãããã¾ããownership ãæèãã¦ã³ã¼ããæ¸ãã¦ããã°å¤§ä¸å¤«ãªãã§ãããå¤é¨ã©ã¤ãã©ãªã¨ã使ã£ã¦ãã¨ä¿¡ç¨ã§ãã¾ãããããâ¦Rust ã®åç¨ã¯ãlifetime ãã³ã³ãã¤ã©ã«ç®¡çããã¦ããåç §ã§ããåç §ãçãã¦ãããã¨ã¯ã³ã³ãã¤ã©ãä¿è¨¼ãã¦ãããã®ã§ãå®å ¨ã«å©ç¨ãããã¨ãã§ãã¾ãã
ã¾ã linearityãéã«ããã¨å¤æ°ãä¸åãã使ããã¦ãªããã©ããã触ã£ã¡ããããªããã®è§¦ã£ã¦ãªãããããã§ãã¯ããã¾ããããã§æ»ãã ãªãã¸ã§ã¯ãã触ã£ã¦ã¢ãã£ã¼ã¨ããäºæ ãéçã«æ¤åºããé²ããã¨ãã§ããããã«ãªãã¾ãã
Rust ã®æ°ããé¨åã¯ãçµå±ã®æã¡ã¢ãªãªã©ã®ãªã½ã¼ã¹ã管çããä¸ã§ãC++ ã§è¶³ããªãã£ãããã£ã¦æ¬²ãããªã¨çãæã£ã¦ãããã®ã¨è¨ã£ã¦ãã¾ã£ã¦ããã§ãããã
C, C++ ã®è©±å¤ããã¦ããããªããã§ãã
Rust ã¯æåã«æ¸ããéããMozilla ã® C++ ã§æ¸ããã巨大ãããã¯ãã®ã³ã¼ããç½®ãæããããè¨èªã§ãªããã°ããã¾ããã§ãããC++ 㯠C++ ãªãã«è²ã é å¼µã£ã¦ããã®ã§ãRust ã¯ããããè¯ããã®ï¼lifetime, move semanticsï¼ãåãåºãã足ããªãé¨åï¼lifetime 㨠linearityï¼ãè£ãã¾ãããã®ã§ã©ããã¦ã C++ ã®è©±ãåºããããå¾ã¾ããâ¦ã¨ãããã¶ã£ã¡ãã Rust ã£ã¦ C, C++ 使ãããã£ãã¼ã¨ããæ°æã¡ã«ãªãè¨èªã ã¨æãã®ã§ãããã§ãªã人ã«è¯ãä¼ããã®ãç¡çã§ããããï¼è«¦å¿µï¼ã
ä¸å¿è«¦ããã«èª¬æé å¼µã£ã¦ã¿ãããGC ã®ããè¨èªã§ããä¸è¦ã«ãªã£ãããã®ã¡ã½ããèªãã§ããã¨ãããããªã¡ã½ãããæã¤ã¯ã©ã¹ã¯ããã¾ãããããé©åãªã¿ã¤ãã³ã°ã§å¼ã¶ã®ãèªæãªããã§å¤§å¤ã§ããããä¸è¦ã«ãªã£ãã¨æã£ããå¥ã¹ã¬ããã§ã¾ã 使ããã¦ããã¨ãããããããã£ããããã£ããã¨ããã人ã¯ãããªãã«ããããããªãã§ããããããããã£ããã¨ã常ã«ãããªãã¨ãããªãã®ã C, C++ ã§ï¼C++ ã¯é å¼µã£ã¦ã¾ããï¼ããããå ¨åã§ãµãã¼ããã¦ãå±éºãªã³ã¼ãã«ãã£ãã¦ãããã®ã Rustãã§ã©ãã§ããããããã£ã±ãé§ç®ãã
ã§ãå ¨åã§ãµãã¼ããã¦ãããã®ã§ããªã½ã¼ã¹ç®¡çã GC ã«ä»»ãã£ããã§çãã¦ãã人ã Rust ãæ¸ãã¨ã大æµå¤ãªã³ã¼ããã§ããããããã§ããâ¦ã¾ãå½ç¶ã§ãããâ¦
ã¾ã¨ã
話ãæ£æ¼«ã¨ããããã®ã§ã¾ã¨ãã¾ãã
- Rust ã® lifetime ã®æ¦å¿µã move semantics ã®è¨èªãµãã¼ã㯠C++ çã§ä»¥åãããã£ããç¹å¥æ°ãããã®ã§ã¯ãªãã
- Rust ã® lifetime ã linearity ã®è¨èªãµãã¼ãã¯ï¼ç 究ç¨ã®è¨èªã¨ãé¤ãã°ï¼æ°ãããã®ã§ããã
- Rust ã® motivation ã« C++ ã絡ãã®ã§ãC++ ç¥ããªã㨠Rust ã®å¬ãããçã«ç解ããã®ã¯é£ããã
ãã¼ããã£ã±ã Rust ã®è¯ããæ°ããã C, C++ ãæ¸ãã¦ããªãã£ã人ãã¡ã«èª¬æããã®ã£ã¦é£ããã§ããâ¦éã«ãããã¾ãã
追è¨ï¼ã¿ããªãæãã§ãããã®ã¨ãæ¸ãã¦ãããã©ä¸ã®ä¸ã«ã¯ã¹ãã使ããªãæ´¾ã¨ã free ããªãæ´¾ãããã®ã§å¤æ§æ§ã§ãã