tl; dr; -version ïŒäžèŠãRustã®äžéšã¯å¥åŠã§äžäŸ¿ã«æãããããããŸããããã·ã¹ãã èšèªãšããŠäœçœ®ä»ããããèšèªã§ã¯éåžžã«æåããŠããããšãããããŸããã æææš©ãšåç¶æéã®æŠå¿µã«ãããèšèªã«åŒ·åãªéçä¿èšŒãããããããã®ããã°ã©ã ãã¡ã¢ãªããéææå¹ãã€å®å šã«ããããšãã§ããŸãã
ã©ã€ã»ã³ã¹ ïŒ ã¯ãªã¹ã»ã¢ãŒã¬ã³ã«ããCC-BY ã
æåã®FizzBuââzzå®è£ ãæ©èœããªãçç±ïŒæåã¯è¡æçã§ããããå®éã«ã¯æé«ã®ãã€ã³ãã§ããRustã®æ©èœã®èª¿æ»ïŒãŸã 奜ãã§ã¯ãªããããããŸãããïŒ
FizzBuââzzã¯åå¿è åãã®åçŽãªå²ãåœãŠãšæ³å®ãããŠããŸãããRustã«ã¯ããã€ãç¥ã£ãŠããã¹ãèœãšãç©ŽããããŸãã ãããã®èœãšãç©Žã¯Rustã®åé¡ã§ã¯ãªããã»ãšãã©ã®ããã°ã©ããŒãããç¥ã£ãŠãããã®ãšã®éãã§ãããäžèŠéåžžã«å³ããå¶éã§ãããå®éã«ã¯äœäŸ¡æ Œã§å€§ããªå©ç¹ãæäŸããŸããRustã¯ãåãæšçãã§ãããèšèªã¯ããå®å®ããŸãã ãã®èšäºã®ã³ãŒãã¯ãããŒãžã§ã³0.12ã§æ©èœããŸãã äœããå£ãããã ç§ã«é£çµ¡ããŠãã ãã ã Pythonã³ãŒãã«é¢ããŠã¯ã2ã€ãš3ã€ã®äž¡æ¹ã§æ©èœããŸãã
ç°¡åãªå®è£
OKãæåã®FizzBuââzzã®å®è£ ã¯æ©èœããªãå¯èœæ§ããããšã¿ã€ãã«ã§èšããŸããã 以äžã®äŸã®ããã«æžãããšãã§ããŸãã ç°¡æœã«ããããã«ã
fn main() { ⊠}
çç¥ã
fn main() { ⊠}
ã Pythonã®ã³ãŒããRustãããçãããšã«æžå¿µãããå ŽåãPythonã«ã¯ç¹å¥ãªåœ¢åŒã®ã³ãŒãããããŸãããã§ãã¯ããã¯ã¹ãã¯ãªãã¯ãããšäœ¿çšã§ããŸãã ïŒ å ã®èšäºã®çŽã¬ãŒã³ãPythonã³ãŒãã
from __future__ import braces
ãç¹å¥ãªåœ¢åŒãã«åãæ¿ãããã§ãã¯ããã¯ã¹ããããŸã ãããã¯èè ã®ã€ãŒã¹ã¿ãŒãšãã°ã®ãããªãã®ã§ã ïŒã
Python | ãã³ |
---|---|
|
|
ã©ã¡ãã®ããã°ã©ã ãæãŸããçµæãçã¿åºããæããã«éåžžã«ãã䌌ãŠããŸãã ããã§èšåãã䟡å€ã®ããäž»ãªããšã¯ãRustã§ã¯
println!()
[1]ãæåã®åŒæ°ãšããŠæååãªãã©ã«ããã©ãŒãããæååãå¿ èŠãšããããšã§ãã 察å¿ããPythonã³ãŒãã¯ã
print('{}'.format(i))
ãŸãã
ããããã³ãŒãå ã®
print
åŒã³åºãã®éè€ãåãé€ãããå Žåã¯ã©ãã§ããããïŒ ããã¯æ¬¡ã®ãããªãã®ã§ãã
Python | RustïŒã³ã³ãã€ã«ãããŠããªãïŒ |
---|---|
|
|
Rustã§if
ãããã¯å šäœãåŒãšããŠäœ¿çšããæ¹æ³ã«æ³šç®ããŠãã ããã å²ãåœãŠã®çµæã§ããæ¬åœã«å¿ èŠã§ã¯ãããŸããããããã¯å šäœã䜿çšããæ§é ã«è©°ã蟌ãããšãã§ããŸãã Pythonã§ã¯ãã¹ãŠãåœä»€ã§ã¯ãªãåŒã§ãããããããã¯ã«ãã¹ãã«ãšã£ãŠã¯éåžžã«ããç¥ãããã¢ãããŒãã§ããããã€ãã¹ãã«ãšã£ãŠã¯ããã§ã¯ãããŸããã ãã®ã¢ãããŒãã«æççã§ããã°ãç§ã¯ç解ããŠããŸãã Rustãç解ãå§ãããšããRustã®åŒã®åããšreturn
ãçç¥ããæ©èœã¯å¥åŠã«æããŸããã ããããRustã䜿çšãããšãããã§ã¯ãªãããšã«æ°ä»ããŸããã ããã¯å®éã«çŽ æŽãããã§ãã
Pythonã¯5幎åããç§ã®ãæ°ã«å ¥ãã®èšèªã§ãããããã®èšèªã§ãããšããŠæžãç¶ããŠããã«ããããããïŒRustã«åãæ¿ããããšæããŸããïŒãRustããããæ¬ ããŠããããšããããããŸãã Rustã䜿çšããå ŽåãRustã«ã¯ãŸã ãªãã©ã€ãã©ãªãå¿ èŠã§ããããšãé€ããŠãPythonã«åãæ¬ ç¹ã¯ãããŸããã äžè¬çã«ãRustã¯ç§ã®ç®ã«Pythonã倧ããéé£ããŸããã
éã³ãã³ãŒãã¯èŠæ ããè¯ãã§ãããå®éã«ã¯ãã®èšèªã®å³å¯ãªå ¥åèŠåã®ããã«æ©èœããŸããã ã§ã¯ã
result
å€æ°ã®ã¿ã€ãã¯äœã§ããïŒ æåã®3ã€ã®
if
ãã©ã³ãã¯æååãè¿ãã4çªç®ã¯æŽæ°ãè¿ããŸãïŒ
f.rs:7:12ïŒ11ïŒ6ãšã©ãŒïŒifãšelseã«äºææ§ã®ãªãåãããå ŽåïŒ `ïŒ 'static str`ãfound` int`ïŒexpectedïŒ-ptrãfound intïŒ f.rs:7} else if iïŒ 3 == 0 { f.rs:8ããã£ãºã f.rs:9} else { f.rs:10 i f.rs:11}; ãšã©ãŒïŒåã®ãšã©ãŒã®ããäžæ¢
ããã¯æ©èœããŸããã æ°åãæååã«å€ããã®ã¯ã©ãã§ããïŒ
for i in range(1i, 101) { let result = if i % 15 == 0 { "FizzBuzz" } else if i % 5 == 0 { "Buzz" } else if i % 3 == 0 { "Fizz" } else { i.to_string() }; println!("{}", result); }
ããã§ã¯ãä»ã®ããã°ã©ãã³ã°èšèªïŒ
to_string
ïŒã§å€ãã®äººã䜿ãæ £ããŠããããããåãåºããããŸãç解ããŠããªãé åã«é©çšããŸããã äžè¬ã«ãããã¯æ©èœããŸããã
f.rs:7:12ïŒ11ïŒ6ãšã©ãŒïŒifãšelseã«äºææ§ã®ãªãåãããå ŽåïŒæåŸ ããã `ïŒ 'static str`ãfound`ã³ã¬ã¯ã·ã§ã³:: string :: String`ïŒexpectedïŒ-ptrãèŠã€ãã£ãæ§é äœã³ã¬ã¯ã·ã§ã³:: string ::æååïŒ f.rs:7} else if iïŒ 3 == 0 { f.rs:8ããã£ãºã f.rs:9} else { f.rs:10 i.to_stringïŒïŒ f.rs:11}; ãšã©ãŒïŒåã®ãšã©ãŒã®ããäžæ¢
ããªã«ïŒããããªãã¯ãã¹ãŠã®è¡ã§ã¯ãªãã®ã§ããïŒããšèšãã®ãèãããŸãã this
&'static str
ïŒäžäœã©ãããããšã§ããïŒïŒAnd
collections::string::String
ïŒ ãã®æ®µéã§ã¯ããã©ã³ãã«ãã£ãŠçæãããå€ã®ã¿ã€ããããæ éã«åæããå¿ èŠããããŸããæåã®3ã€ã®ãã©ã³ãã¯ãããçš®ã®ãæååããçæããã ãã§ãªãã
&'static str
ãçæãã4çªç®ã®ãã©ã³ãã¯åãªãæŽæ°ã§ã¯ãªã
int
ãŸãã PythonãRubyãJavaScriptãªã©ã®èšèªã§ã¯ãæŽæ°åãçµåããïŒJSã¯ããã«é²ãã§ããã¹ãŠã®æ°å€åãå®å šã«çµ±åããŸããïŒãCïŒãJavaãGoã«ã¯ãµã€ãºãç°ãªãæŽæ°åãå€æ°ãããŸãã ããããCïŒãJavaãGoãªã©ã®èšèªã§ãããæååã«å¯ŸããŠ1ã€ã®ã¿ã€ããããããŸããã
ããããéã¯ããã§ã¯ãããŸããã 圌ã«ã¯2ã€ãããŸãã
2çš®é¡ã®æååïŒ ããã¯äœã§ããïŒ
ããã§ã¯ç°¡åãªèª¬æã«éå®ããŠå ã«é²ãããšãã§ããŸãããæ·±ãæãäžããã®ã§ãæåŸãŸã§è¡ã£ãŠäœãè¡ããããªãããã絶察ã«äŸ¡å€ãããã®ãââãç解ããŠãã ããã ããã§ã¯ããªãCâ¯ãJavaãGoã¯åãæååã¿ã€ãã§æºè¶³ã§ããã®ã«ãRustã§ã¯æºè¶³ã§ããªãã®ã§ããããïŒ ãã®è³ªåã«çããã«ã¯ãã¡ã¢ãªç®¡çã®ã¬ãã«ãŸã§äžããå¿ èŠããããŸãã
Câ¯ãJavaãGoã¯ãã¹ãŠãããŒãžãèšèªã§ã [2] ïŒ ã¬ããŒãžã³ã¬ã¯ã·ã§ã³èšèªãšãåŒã°ããŸãïŒã ã€ãŸããé©åãªã¿ã€ãã³ã°ã§ã¡ã¢ãªã®å²ãåœãŠãšå²ãåœãŠè§£é€ãå¶åŸ¡ããå®è¡æã®ã¡ã«ããºã ããããŸããä»ã«èª°ãæååã䜿çšããªãå Žåãããã¯ç¹å¥ãªå ŽåããããŸãã ãããã£ãŠãæååãžã®ãªã³ã¯ããã®å¯¿åœãæ°ã«ããã«è¿ãããšãã§ããŸãããŸã 䜿çšäžã®æååã¯ç¹å¥ãªãã®ã§ã¯ãããŸããã
ãããã®èšèªã«ã¯1ã€ã®è²æ©ããããŸãã ååãšããŠããããã«ã¯äžå€ïŒäžå€ïŒã®è¡ããããŸã-2ã€ã®è¡ãé£çµãããŠããå Žåãå¿ èŠãªãµã€ãºã®æ°ããè¡ã«ã¡ã¢ãªãå²ãåœãŠãããŸãïŒå²ãåœãŠïŒã ïŒããã¯ãé©åãªå ŽåãFizzãšBuzzãé£çµãããœãªã¥ãŒã·ã§ã³ã§ã¯ã15ã§å²ãåããæ°å€ã«2ã€ã®å²ãåœãŠãããããšãæå³ããŸãã確ãã«ãäžéšã®èšèªã§ã¯ã æååããŒã«ãŸãã¯æçãšåŒã°ãããã®ã䜿çšããŠããã®æªåœ±é¿ãå°ãæ»ããã«ããããšãã§ããŸããã®ã¡ã«ããºã ã®æåã¯ããªããã£ãã€ã¶ãŒãšã³ãŒãã®èšè¿°æ¹æ³ã«äŸåããŸãïŒä»£ããã«ããã倧ããªæªããããããè¡ã¯äžå€ã§ãããšæããŸã-è¡ãå€æŽãããšãããã«å¿ããŠä»ã®è¡ã«åœ±é¿ãäžããå¯èœæ§ããããŸãã ããã¯ãããã°ã©ã ã®æ£ç¢ºãã«å€§ãã圱é¿ããåºæ¬çã«ããªããã£ãåã§ãã競åç¶æ ã«ã€ãªããå¯èœæ§ããããŸãã
ãŸãããŠãã³ãŒãè¡ã®å Žåãããã«ããäžæ£ç¢ºãªè¡ã«ãããçºçããå¯èœæ§ããããŸãã ãã¡ããããããã®åé¡ã¯ä»ã®å Žæã§ãçºçããŸãããããããäžåã«äžŠã¹ããšãããã«æªåããå¯èœæ§ããããŸãã ïŒãããã®èšèªã«ã¯1ã€ã®æåååããããšèšããŸããããããã¯å®å šã«çå®ã§ã¯ãããŸãã
StringBuilder
ãš.NETã®äž¡æ¹ã«
StringBuilder
ãšåŒã°ããã¡ã«ããºã ããããªã©ãç¹æ®ãªæåååããããŸãïŒã
Rustã¢ãã«ã¯ãã¬ããŒãžã³ã¬ã¯ã·ã§ã³èšèªã§äœ¿çšãããã¢ãã«ãšã¯ç°ãªããæææš©ã®æŠå¿µã«åºã¥ããŠããŸãã ãã®ã¢ãã«ã§ã¯ãåãªããžã§ã¯ãã«ã¯äžåºŠã«1人ã®ææè ïŒ ãããLaneãææãããŠããŸã ïŒããããä»ã®å Žæã§ã¯ãã®ãªããžã§ã¯ããžã®ãã€ã³ã¿ãŒãå®å šã«ååŸã§ããŸãã
collections::string::String
ã¯æææš©ãæã€åã§ãã ããã¯ã圌ãæ ªã®å 容ãææããç¬å çãªæš©å©ãæã£ãŠããããšãæå³ããŸãã ãã®ã¿ã€ãã®ãªããžã§ã¯ããã¹ã³ãŒããåºããšãè¡ã¯è§£æŸãããŸãã ãããã£ãŠãæååãšéšåæååã®éã«æ¥ç¶ããªããããéšåæååã
String
åã«ããããšã¯ã§ããŸãããæåã®éšåæååãã¹ã³ãŒããé¢ãããšã2çªç®ã®éšåæååãäžæ£ç¢ºã«ãªããŸãã 代ããã«ãéšåæååïŒãŸãã¯æååã¹ã©ã€ã¹ ïŒã¯ãä»ã®èª°ããææãããªããžã§ã¯ããžã®åç §ã§ããåã䜿çšããŸã-
&str
ã Rustã¯ããªããžã§ã¯ãã®ã©ã€ãã¿ã€ã ã®æŠå¿µã®ãããã§ãåäžã®è¡ã¹ã©ã€ã¹ãå ã®æååãçãæ®ããªãããšãä¿èšŒã§ãããããã¡ã¢ãªã®å®å šæ§ãç¶æãããŸãã
ç涯ã¬ã€ãã«ã¯ããã詳现ãªèª¬æããããŸã ã ããã§ãåç §ã¿ã€ãã®åŸã«
'_
ã®æ§é ã衚瀺ãããå Žåãããããªã³ã¯ã®æå¹æéã®æ±ºå®æ¹æ³ã§ããããšã«æ³šæããŠãã ããã ç¹å¥ãª
'static
åç¶æéããããŸããããã¯ããªããžã§ã¯ããããã°ã©ã å šäœã«ååšããããšãæå³ããŸãã ãã®ãããªãªããžã§ã¯ãã¯ãã³ãŒãå ã§èŠã€ãã£ãæååãªãã©ã«ãšåæ§ã«ãå®è¡å¯èœãã¡ã€ã«ã«çŽæ¥ãã€ã¯åŠçãã
&'static str
ãã€ãŸããæååãªãã©ã«å
&'static str
ã§ãã
以åã¯ãstr
ãBox<T>
ã§ãããstr
ãåœã§ãã£ãå Žåãtypeãstrã¯ãµã€ãºå€æŽå¯èœãªæåååã§ããã çŸåšã®ïŒãµã€ãºïŒãšæ倧ïŒå®¹éïŒã®ãµã€ãºããString
ã®çŸåšã®ã¿ã€ãïŒ~str
眮ãæããããïŒãšããŠä¿åããŸããã ãã¹ãŠã®ã©ãããŒã¿ã€ãã¯ããã®æ¹æ³ã§åäœããã¯ãã§ããã çŸåšãBox<T>
ã¯åçŽãªã©ãããããå€ã§ãã ããã䜿çšãããªãçç±ã§ã-è¿œå 容éãªãã§ã¯ãæååã«è¿œå ããããã³ã«ã¡ã¢ãªãåå²ãåœãŠããå¿ èŠããããŸããString
ã¯ã¡ã¢ãªãåå²ãåœãŠã§ããããã©ã«ãã§ãããè¡ããŸãã ãããã£ãŠãBox<str>
ãš&str
ã®éã&str
éèŠã§ãã
ãã®å€æŽäžã«ãæ°ããåãStrBuf
ãšåŒã°ããããšãè¿œå ã§ããŸãã å®éãç¶æ³ã¯ä»ã®èšèªã®ç¶æ³ãšå€§å·®ãããŸããã å®éãããã¯åŒ·å¶çãªã¬ããŒãžã³ã¬ã¯ã·ã§ã³ã®æ¬ åŠã®åœ±é¿ã§ãããäžéšã®&str
圹ã«ç«ããªããã®ã«ãªããŸãã Rustã§ã¯ãä»ã®èšèªã§ã¯ã¡ã€ã³ã®æååã¿ã€ãã«ç°¡åã«ã¢ã¯ã»ã¹ã§ãããããä»ã®èšèªãããæååãããã¡ã«å°ãé »ç¹ã«ã¢ã¯ã»ã¹ããå¿ èŠããããŸãã
FizzBuââzzã«æ»ã
ã€ãŸããåé¡ã¯ã1ã€ã®ãã©ã³ãã«æææš©ãæã€è¡ããããä»ã®3ã€ã®è¡ã¯éçãªæååã¹ã©ã€ã¹ïŒéçã«å®çŸ©ãããæååãžã®ãªã³ã¯ïŒã§ãããšããããšã§ãã ãã®åé¡ãã©ã®ããã«è§£æ±ºããŸããïŒ ãã¶ãããã¹ãŠã®æååã¹ã©ã€ã¹ãäœæããããšããŸãïŒã¯ããä»»æã®ã©ã€ãã¿ã€ã
'a
&str
ã¿ã€ã
'b
ã
'b
ããé·ãå Žåãæé»çã«
'b
ãã£ã¹ãã§ããŸãã'staticã¯äœãããé·ãã®ã§ãã³ã³ãã€ã©ã¯èªç±ã«å®è¡ã§ããŸããé©åãªã©ã€ãã¿ã€ã ã«å€æããŸãïŒïŒ
for i in range(1i, 101) { let result = if i % 15 == 0 { "FizzBuzz" } else if i % 5 == 0 { "Buzz" } else if i % 3 == 0 { "Fizz" } else { i.to_string().as_slice() }; println!("{}", result); }
ããèãã§ããã ç³ãèš³ãããŸãããããããæ©èœããŸããïŒ
f.rs:10:9ïŒ10:22ãšã©ãŒïŒåçšããå€ã¯ååã«é·ãçããŠããŸãã f.rs:10 i.to_stringïŒïŒãas_sliceïŒïŒ ^ ~~~~~~~~~~~~~ f.rs:2:25ïŒ13ïŒ2泚ïŒåç §ã¯2:24ã®ãããã¯ã«å¯ŸããŠæå¹ã§ãªããã°ãªããŸãã... f.rs:2 for i in rangeïŒ1iã101ïŒ{ f.rsrouble let let = if iïŒ 15 == 0 { f.rs:4 "FizzBuââzz" f.rscla} else if iïŒ 5 == 0 { f.rs:6ãããºã f.rs:7} else if iïŒ 3 == 0 { ... f.rs:9:12ïŒ11ïŒ6泚ïŒ...ãã ããåçšããå€ã¯9:11ã®åŒã§ã®ã¿æå¹ã§ã f.rs:9} else { f.rs:10 i.to_stringïŒïŒãas_sliceïŒïŒ f.rs:11}; ãšã©ãŒïŒåã®ãšã©ãŒã®ããäžæ¢
ããã§ç§ãã¡ã¯çåœã«
i.to_string()
ãŸãïŒ
i.to_string()
çæãããè¡ã¯ååãªæéä¿åãããããããã¯ã®çµããã«è§£æŸãããŸãã ãããã£ãŠããããžã®ãªã³ã¯ããããã¯ãé¢ããããšãã§ããŸããã ããã¯ãRustã³ã³ãã€ã©ãŒãæ£åžžã«ãã£ããããç¡å¹ãªã¡ã¢ãªãŒãžã®ãªã³ã¯ã«é¢é£ããæœåšçãªãã°ã§ãã äžéšã®èšèªã§ã¯ãããã¯ããã³ã°ãªã³ã°ãã€ã³ã¿ãŒããšåŒã°ããéåžžã«æªãã§ãã
ããã§ããããã¯ããšã«æååå€æ°ãåçŽã«äžããããšãã§ããŸããã«ãŒãã®æ¬äœã§æååãæå¹ã§ããã°ååã§ãã æã«ã¯ããã§ååã§ãããå€ãã®å Žåã¯ããã§ã¯ãªãç¶æ³ã«ééããããšããããŸãã
for i in range(1i, 101) { let x; let result = if i % 15 == 0 { "FizzBuzz" } else if i % 5 == 0 { "Buzz" } else if i % 3 == 0 { "Fizz" } else { x = i.to_string(); x.as_slice() }; println!("{}", result); }
ãªã³ã¯ãå²ããããã¯ã«é
眮ããŸããããã¯æ©èœããŸãã
ãã¹ãŠãString
ãŸãããïŒ
éæ¹åã«é²ããšããã¹ãŠã®ãã©ã³ããæææš©ãæã€è¡ãè¿ãå¿ èŠããããŸãã
for i in range(1i, 101) { let result = if i % 15 == 0 { "FizzBuzz".to_string() } else if i % 5 == 0 { "Buzz".to_string() } else if i % 3 == 0 { "Fizz".to_string() } else { i.to_string() }; println!("{}", result); }
ç§ãã¡ã¯ãã¹ãŠãäžåã«è¡ããŸãããå®è¡æã«ç¡æã§ã¯ãããŸããã
ãã®ã¢ãããŒãã¯ããŸãæ©èœããŸãããæ°å€ãååŸãããã®ã ãã§ãªããå埩ããšã«ã¡ã¢ãªãå²ãåœãŠãããããšãæå³ããŸãã
æžã蟌ã¿æ©èœ
ã³ãŒããäžæ¡çã«è»¢ããããšãªããã§ããã ããã®æ¹åã«é²ã¿ãŸããã åé¡ã®ãŸãã«ã¹ããŒãã¡ã³ããã€ãŸããçµæãåºåããã«é¢æ°ããè¿ããšããã¹ããŒãã¡ã³ããå€æŽããã®ã¯ã©ãã§ããïŒ
ãã®ã³ãŒãããå§ããŸãããïŒ
Python | ãã³ |
---|---|
|
|
ïŒ èªåã§è©ŠããŠã¿ãããšãã§ããŸã ãé©åãªã©ã€ãã¿ã€ã ããªããããé¢æ°ã®æ»ãå€ãRuståã·ã¹ãã ã§è¡šãããšãã§ããŸãã
x
ã¯
'static
ã©ã€ãã¿ã€ã ãååŸã§ãããã¢ã¿ããã§ãããã®ããããŸããããïŒ
ãŸããé¢æ°ã«ã³ãŒããé 眮ãããããå¿ èŠã®ãªãå Žåã«ã¯æ°ããè¡ãéžæããŸãã
SendStr
玹ä»
幞ããªããšã«ãRustã¯ä»£æ°ããŒã¿å ïŒ
enum
ãåŒã°ããŸã ïŒããµããŒãããŠããŸãã ãŸããæšæºã©ã€ãã©ãªã«ã¯ãæååã¹ã©ã€ã¹ãŸãã¯æææš©æååã®ããããã§ãããªããžã§ã¯ããèšè¿°ããããšãã§ããé©åãªã¿ã€ãããããŸãã
以äžã¯ããã®ã¿ã€ãã®å®çŸ©ã§ãïŒããã«äŸ¿å©ã«ãªãã¡ãœããã®èª¬æã¯ãããŸããïŒïŒ
pub enum MaybeOwned<'a> { Slice(&'a str), Owned(String) } pub type SendStr = MaybeOwned<'static>;
std::str
ã®
MaybeOwned
ããã³
SendStr
ã
Send
ã¯ãã¿ã¹ã¯éã§ïŒã€ãŸããã¡ã¢ãªã®å®å šæ§ã倱ãããšãªãã¹ã¬ããéã§ïŒãªããžã§ã¯ããå®å šã«è»¢éã§ããããšã瀺ãå¶éã§ãã ãŸãããªããžã§ã¯ããèªå·±å®çµåã§ãããé¢æ°ããè¿ãããšãã§ããããšãæå³ããŸãã
SendStr
ã®å®çŸ©ã®ããã«ã
&'static str
ãããªæååããããšããŸãã é¢æ°å ã®ãªããžã§ã¯ããžã®åç §ã¯å«ãŸããŠããŸããããïŒ ãããã£ãŠãå¿ èŠãªéãååšã§ããŸãã åãããšã
String
ãåœãŠã¯ãŸããŸãã ãããã£ãŠããããã®2ã€ã®ãªããžã§ã¯ãã®ããããã
enum
åã§ãã£ããã£ã§ããŸããããã¯ãããããã®ãªããžã§ã¯ããææããŠããããšã瀺ããŸãã ãããã£ãŠã
SendStr
ã¯
Send
æ¡ä»¶ãæºãããŸãã ãã®ã¿ã€ãã¯ç¹å®ã®å€ãæ ŒçŽãããŠãŒã¶ãŒã¯ãã®äžã§ããŸããŸãªæäœãå®è¡ã§ããŸã ã ä»æã泚ç®ãã¹ãäºå®ã¯ã
as_slice()
ã䜿çšããŠãã®åããæååã¹ã©ã€ã¹ãæœåºã§ããããšã§ãã ãã®åã¯ã
std::fmt::Show
ãå®è£ ã
std::fmt::Show
ãã€ãŸãã
{}
æå®ããããšã§ãæžåŒä»ãåºåã§çŽæ¥äœ¿çšã§ããŸãïŒ
Show
åã¯ãPythonã®
__str__()
ãŸãã¯
to_s()
ã
toString()
ã
&c
çŽæ¥ã®é¡äŒŒäœ
__str__()
ä»ã®èšèªã§ã¯ã
writer
ãªããžã§ã¯ããšçŽæ¥é£æºãããããäžéæååãªããžã§ã¯ãã
to_string()
ã§ããŸã
to_string()
ãå®è£ ããåã§
to_string()
ãåŒã³åºããšããã®ã¡ã«ããºã ãåŒã³åºãããŸãïŒã
ã¢ããªã±ãŒã·ã§ã³ã¯æ¬¡ã®ããã«ãªããŸãã
use std::str::SendStr; fn fizz_buzz(i: int) -> SendStr { if i % 15 == 0 { "FizzBuzz".into_maybe_owned() } else if i % 5 == 0 { "Buzz".into_maybe_owned() } else if i % 3 == 0 { "Fizz".into_maybe_owned() } else { i.to_string().into_maybe_owned() } } for i in range(1i, 101) { println!("{}", fizz_buzz(i)); }
fizz_buzzé¢æ°ã¯
SendStr
è¿ããŸãã åäœããŸãã
ïŒ
.into_maybe_owned()
ãã
IntoMaybeOwned
ãããããã©ã«ãã§å©çšå¯èœ ïŒ
ãã£ãããïŒ ããã§ãã³ã³ãã¥ãŒã¿ãŒãå®è¡ããå¿ èŠã®ããäœæ¥éãåæžãããããç¥ãããäŸãé«éåãããŸããã
ããããããã«å ãžé²ãããšãã§ããŸããïŒ
ç¬èªã®enum
åãäœæãã std::fmt::Show
ãå®è£
ãã
ãã¡ãããéä¿¡ããã®ã¯å®éã«ã¯ãæååãã§ã¯ãªãããããã¯ãFizzãããBuzzãããFizzBuââzzãããŸãã¯æ°å€ã®ããã€ãã®å€ã§ãã ãã¹ãŠã®ãªãã·ã§ã³ãäºåã«æååã«å€æããã ãã§ãã äžèŠãªå²ãåœãŠãåé¿ããŠããããç°¡åã«é 延çã«è¡ãããšãã§ããŸãïŒå®éãããã§ã®ãã¹ãŠã®å²ãåœãŠã¯åé¿ã§ããŸãïŒã
ç¬èªã®
enum
äœæããŸãããã
ãã®ããã»ã¹ã§ã¯ã
std::fmt::Show
ãå®è£ ããŸããããã«ãããäžéè¡ãå¿ èŠãšããã«
stdout
ã«çŽæ¥åºåã§ããŸãã
Pythonã®åçç©ïŒéåžžã«ã¿ã€ãïŒ | ãã³ |
---|---|
|
|
ããã¯ããŒã¿ã衚瀺ããã®ã«æ¬åœã«è¯ãæ¹æ³ã§ããããšã«æ³šæããŠãã ããããã ãããã®å Žåã¯ããŸãæ°ã«ãããæåã®3ã€ã®ãã©ã³ãã
Word(&'static str)
ã¿ã€ãïŒ
Word("FizzBuzz")
ãªã©ã«çœ®ãæããã ãã§ãã ïŒå®éã«ã¯ãããã¯ç§ããã®ã¹ãããã§æžããæåã®ããŒãžã§ã³ã§ããããããäžèŠãªæååã䜿çšããŠãªã³ã«ããã®ã§ãïŒïŒ
å¥ã®ã€ãã¬ãŒã¿ãæžãããšã§ããã«é²ããããšãã§ããŸãããRustã§ã€ãã¬ãŒã¿ãæ©èœããæ¹æ³ãèãããšãããã¯å®å šã«ãªãã·ã§ã³ã§ã-
range(1, 101).map(fizz_buzz)
ç°¡åã«æžãããšãã§ããŸãã ããã«ãããæè»æ§ãå€§å¹ ã«åäžããŸãã
Iterator<int>
ãã©ããã«å®è£ ãããããæåŸã«
.map(fizz_buzz)
ãè¿œå ããã ãã§ã
Iterator<FizzBuzzItem>
ãå®è£ ããåãååŸã§ããŸãã
ãµã€ã¯ã«ã¯ããã®ã¹ã¿ã€ã«ã§1åãŸãã¯2åæžãæããããšãã§ããŸãã
Python | ãã³ |
---|---|
|
|
ã©ã¡ãã®æ¹æ³ãéžæãããšããŠããçµæãšããŠãå€ãè¯ãFizzBuââzzããã°ã©ã ã䜿ãæãããŠããŸããŸãã
ãããã«
ããã§ãæåã®Rust FizzBuââzzå®è£ ãæ©èœããªãçç±ãããããŸããã ãã®èšäºã§èª¬æãããŠããé£ããã®ããã€ãã¯éçã«åä»ããããèšèªã«å žåçã§ãããããã€ãã¯Rustã«åºæã®ãã®ã§ãã ïŒå®éãC ++ã®ç¶æ³ã¯C ++ã®ç¶æ³ãšäŒŒãŠããŸãããC ++ã䜿çšãããšãã«ãªééããããããã§ããããã«ãªããã¡ã¢ãªãæäœããä¿èšŒããããŸãããããã§ä»ã®äººãåŒçšããŠããã ãã§ããç§ã¯C ++ãæ£ããç¥ããŸãããïŒ
Rustã®æææš©ã¢ãã«ã®ãããã¯ãšããããã©ã®ããã«ããªããæ £ããŠããã¹ã¿ã€ã«ã§æžãããšãé²ãããšãã§ãããããããŠãªãããããã®ãã調ã¹ãŸããïŒãã ããç¹å®ã®å©ç¹ã«ã€ããŠã¯èª¬æããŸããïŒã ãŸãã
enum
åïŒä»£æ°ããŒã¿åïŒã®å¹æçãªæŠå¿µã«ã€ããŠãèšåããŸãããããã«ãããããŒã¿ãããå³å¯ãã€å¹ççã«èšè¿°ããããšãã§ããŸãã
ããããã¹ãŠã®åãã芧ããã ããèå³ãæã£ãŠããã ããããšãé¡ã£ãŠããŸãã
説æãããŠããè¿œå ã®ã»ãã³ãã£ãã¯ããŒãã¯ãããŸããïŒ ã¯ã
äžå¿«ã§ããïŒ å®æçã«ã ïŒç§ã®çµéšã«ããã°ãããã¯ãã¹ãŠããããäœæãããã®ãšåããããé »ç¹ã«å°é£ããæãããŸããïŒ
ããã«ãããããã°ã©ã ã®å¹çãåäžããŸããïŒ ãã¡ããããããŠå®å šãªèªä¿¡ãæã£ãŠã 以åã¯ããããã®ããšã«ã¯ã»ãã¥ãªãã£ãšæ£ç¢ºæ§ã®æ倱ãå¿ èŠã§ããããçŸåšRustã§ã¯ããã®ãããªåŠ¥åã¯å¿ èŠãããŸããã
ããã¯ã³ãŒãã®äŸµå ¥ãç°¡åã«ããŸããïŒ ãã®ãããªåçŽãªã±ãŒã¹ã§ã¯å€§ããªéãã¯ãããŸããããè€éãªã±ãŒã¹ã§ã¯ãããã®ã¡ã«ããºã ãæ¬åœã«åœ¹ç«ã¡ãŸãã ïŒPythonã§ããããæ¬åœã«æããã§ããïŒ
ãããã®æŠå¿µããŸãšãããšãè¯ãé¢ãšæªãé¢ããããšèšããŸããæã«ã¯ããªãã¯ããããæããæã«ã¯ããªãã¯ããããå«ããŸããããããå°ãªããšãç§ã¯ããããããŸãé »ç¹ã«å«ããŸããã
Rustã䜿çšããå¿ èŠããããŸããïŒãŸããå°ãªããšãè©ŠããŠã¿ãããšããå§ãããŸããã·ã¹ãã ããã°ã©ãã³ã°ã«éç¹ã眮ãããŠãããããæªå å·¥ãŸãã¯ç®çã«é©ããªãå ŽåããããŸããå€ãã®é«ã¬ãã«ã®ã¿ã¹ã¯ã§ã¯ãå€å°é¢åã§ããããããç§ã¯æãæ¥ãŠãWebããã°ã©ãã³ã°ã®ãããªãã®ã®ããã®ã¯ãŒã«ãªããŒã«ã«ãªããšä¿¡ããŠããŸããããã¯StrangeLoopã§ã®è¬æŒã§è©±ããŸããïŒã¹ã©ã€ããã芧ããã ããŸãã2MB SVGïŒã
æåŸã«ãRustãåããŠäœ¿çšããå ŽåããŸãã¯èšäºã®äžéšãç解ããŠããªãå Žåã¯ãå ¬åŒããã¥ã¡ã³ãã«æ £ããããšããå§ãããŸãã; Rustã®30åéã®çŽ¹ä»ã§ã¯ãæææš©ã®æŠå¿µãéåžžã«ãã説æãããŠããŸãããŸããHydeã«ã¯ãããç¥ããã
enum
ã¿ã€ããªã©ããããŸããç¹å®ã®åé¡ã«é¢ãã詳现ãªã¬ã€ãããããŸãããŸã 質åãããå Žåã¯ãirc.mozilla.orgã®#rustãã£ã³ãã«ã®ãããªå Žæã倧ãã«åœ¹ç«ã¡ãŸã-ç§ã¯é·ãéããã«ããŸããç§ã®ããã¯ããŒã ã¯ChrisMorganã§ãã
FizzBuââzzã®æé©åããããã®ãæ¬åœã«å¥œããªã
ã¯ãããé¡ãããŸããããã¯æçµããŒãžã§ã³ã§ãããRustã®ææ°ããŒãžã§ã³ã§ã³ã³ãã€ã«ããããã«å¿ èŠãªæå°éã®ä¿®æ£ãšã
OUT
èªã¿ããããåäžãããããã®æååããŒãžã§ã³ïŒïŒïŒïŒããããŸãã
#![no_std] #![feature(asm, lang_items)] extern crate libc; static OUT: &'static [u8] = b"\ 1\n2\nFizz\n4\nBuzz\nFizz\n7\n8\nFizz\nBuzz\n11\nFizz\n13\n14\nFizzBuzz\n\ 16\n17\nFizz\n19\nBuzz\nFizz\n22\n23\nFizz\nBuzz\n26\nFizz\n28\n29\nFizzBuzz\n\ 31\n32\nFizz\n34\nBuzz\nFizz\n37\n38\nFizz\nBuzz\n41\nFizz\n43\n44\nFizzBuzz\n\ 46\n47\nFizz\n49\nBuzz\nFizz\n52\n53\nFizz\nBuzz\n56\nFizz\n58\n59\nFizzBuzz\n\ 61\n62\nFizz\n64\nBuzz\nFizz\n67\n68\nFizz\nBuzz\n71\nFizz\n73\n74\nFizzBuzz\n\ 76\n77\nFizz\n79\nBuzz\nFizz\n82\n83\nFizz\nBuzz\n86\nFizz\n88\n89\nFizzBuzz\n\ 91\n92\nFizz\n94\nBuzz\nFizz\n97\n98\nFizz\nBuzz\n"; #[start] fn start(_argc: int, _argv: *const *const u8) -> int { unsafe { asm!( " mov $$1, %rax mov $$1, %rdi mov $0, %rsi mov $$0x19d, %rdx syscall " : : "r" (&OUT[0]) : "rax", "rdi", "rsi", "rdx" : ); } 0 } #[lang = "stack_exhausted"] extern fn stack_exhausted() {} #[lang = "eh_personality"] extern fn eh_personality() {} #[lang = "fail_fmt"] extern fn fail_fmt() {}
翻蚳è ã®ã¡ã¢ïŒ
1. Rustã«ã¯éçºããããã¯ãã·ã¹ãã ãããããã®å Žå
println!
ã¯ã³ã³ãã€ã«æã«ç¹å®ã®ã¿ã€ãã«ç¹åããåŒã³åºãã§å±éãã
println
ãŸãã
2.åããŠãªãªãžãã«ãèªããšãããããŒãžãã³ãŒãã«ã€ããŠè©±ããŠãããããªå°è±¡ãåãããããããŸããããããã¯ãããŒãžãã¡ã¢ãªãæããŸããæ¬åŒ§ã®å åŽãšå€åŽã®ããŸããŸãªå®åŒåã«ãããããããç§ãã¡ã¯åãããšã«ã€ããŠè©±ããŠããã
çŽ æã¯ååã«å€§ããããã翻蚳ã®æäœçãŸãã¯æå³çãªãšã©ãŒã¯ããªãå¯èœã§ãããŸããç§ã¯Rustããã³éçã«åä»ããããèšèªã®å°é家ã§ã¯ãªããšããäºå®ã«ãããäžéšã®ã¡ã«ããºã ã®èšè¿°ã«äžæ£ç¢ºããçããå¯èœæ§ããããŸããã©ã¡ãã®å Žåãããã©ã€ããŒãã¡ãã»ãŒãžã§ä¿®æ£ãéã£ãŠããã ããã°å¹žãã§ãã
ãæž èŽããããšãããããŸããã