æææš©ãªããç¥ãã
Rustã«ãããæææš©ãšã¯ãã¡ã¢ãªãŒãå®å šã«ç®¡çããããã®æ©æ§ã§ããã¡ã¢ãªãŒã«ä¿åãããå€ã«ã¯ææè ãšåŒã°ããå€æ°ãèšå®ãããŸããææè ã¯ããäžæç¹ã«ãããŠå¿ ãäžäººïŒã€ãŸãäžã€ã®å€æ°ïŒã§ãããææè ãã¹ã³ãŒãããå€ãããªã©äžèŠã«ãªã£ãããææè ãã¡ã¢ãªãŒäžã®å€ãç Žæ£ããŸãã
fn main() {
// ã¡ã¢ãªãŒã«ãHello Rustããšããæååãæ ŒçŽããã
// æååãHello Rustãã®ææè
ã¯å€æ°str
let str = String::from("Hello Rust");
println!("greeting : {}", str);
}
// ææè
ã§ããstrãã¹ã³ãŒããæããããšã§ãã¡ã¢ãªãŒã®ãHello Rustããç Žæ£ããã
ãã®æææš©ã«ã€ããŠãã£ãšèª¿ã¹ãŠã¿ãŸããã®ã§èšäºã«ããŸããæææš©ãšã¯çŽæ¥é¢ä¿ãªãæãžè©±ãéžããŠããç®æãç¹åšããŠããŸãããŸããååŠè ã®åŠç¿ã¡ã¢ã§åŸ®åŠã«åãã£ãŠããªãéšåããããŸãããã®èŸºã¯æªãããã ðŠðŠ ïŸïŸïŸïŸã
ã¡ã¢ãªãŒ
æææš©ã®è©±ã«ã¯ã¡ã¢ãªãŒã絡ãã§ããŸãã®ã§ããŸãã¯ããããåŠç¿ããŸãã
Rustã¯ã¡ã¢ãªãŒã以äžã®ããã«å€§å¥ãå©çšããŸãã
- â éçé å
- â¡ ããã¹ãé å
- ⢠ã¹ã¿ãã¯é å
- ⣠ããŒãé å
â ã®éçé åã«ã¯ãã°ããŒãã«å€æ°ãæååãªãã©ã«ãæ ŒçŽãããŸãããŸããâ¡ã®ããã¹ãé åã«ã¯ãã³ã³ãã€ã«æã«çæããããã€ããªãŒã³ãŒããæ ŒçŽãããŸããéçé åãšããã¹ãé åã¯ä»åã®ã話ã«ã¯ç»å Žããªãã®ã§å¿ããããšã«ããŸãã
â¢ã®ã¹ã¿ãã¯é åã«ã¯ãé¢æ°å ã®ããŒã«ã«å€æ°ãªã©ã³ã³ãã€ã«æç¹ã§å¿ èŠãªã¡ã¢ãªãŒã®ãµã€ãºãå€æããããŒã¿ãæ ŒçŽãããŸããã¹ã¿ãã¯é åã¯ããããåŸå ¥ãå åºãæ¹åŒã§ããŒã¿ã管çããŠããã容éã¯éãããŠãããã®ã®é«éãªã¢ã¯ã»ã¹ãå¯èœã§ãããã®ã¹ã¿ãã¯é åãä»åã®ã話ã§ã¯ãããªã«ç»å ŽããŸããã
ãããŠãæ®ãâ£ã®ããŒãé åã«ã¯ãã³ã³ãã€ã«æã«å¿
èŠãªã¡ã¢ãªãŒã®ãµã€ãºãåãããªãããŒã¿ãæ ŒçŽãããŸããäŸãã°String
ãVec
ãªã©ãããã«ããããŸãã
ã¹ã¿ãã¯é å
ç¹°ãè¿ãã«ãªããŸãããã¹ã¿ãã¯é åã«ã¯ã³ã³ãã€ã«æã«å¿ èŠãªã¡ã¢ãªãŒé åã®ãµã€ãºãåããããŒã¿ãæ ŒçŽããã®ã«å¯ŸããããŒãé åã«ã¯å®è¡æã«å¿ èŠãªãµã€ãºãåããïŒè£ãè¿ãã°ã³ã³ãã€ã«æã«ãµã€ãºãåãããªãïŒããŒã¿ãæ ŒçŽããŸãã
ã¹ã¿ãã¯ã¯ç©ã¿äžããªã©ãšèš³ãããŸããã以äžã®ç»åã®ãããªçŽ°ãç®±ã®ã€ã¡ãŒãžã§ããã¹ã¿ãã¯é åã«ããŒã¿ãä¿åãããšãã¯1çªäžã«ç©ã¿äžããŸããããŒã¿ãåãåºãæã¯1çªäžããåãåºããŸãã
ä»åã®èšäºã®ããŒããšã¯çŽæ¥é¢ä¿ãããŸããããRustã«ãããŠã¹ã¿ãã¯ã«ããŒã¿ãç©ãŸãããšããã®ãã©ã®ãããªæãããè©ŠããŠã¿ãããšæããŸãã
Rustã§ã¯ãããŒã¿åã«ãã£ãŠããŒã¿ãã¹ã¿ãã¯ã«ç©ãŸãããããŒãã«æ ŒçŽããããã決ãŸããŸããã¹ã¿ãã¯ã«ç©ãŸããããŒã¿åãšããŠãä»åã¯æ°å€ãè¡šãi32
ãšf64
ãäŸã«ãšããŸãã
i32
ã¯32bitãã€ãŸã4ãã€ãã§ããŒã¿ãè¡šçŸããŸãã以äžã®ããã«å€æ°ã«å€ãæçžããã°ãåæšè«ãåãi1
ãši2
ã¯å
±ã«i32
åãšãªããŸãã
fn main() {
// å
±ã«i32å
let i1 = 1;
let i2 = 2;
}
ãã®ã³ãŒãã§ããã°ãi1
âi2
ã®é ã§ã¹ã¿ãã¯ã«ç©ãŸããŸãã
ãããã®å€æ°ãç©ãŸããŠããã¹ã¿ãã¯ã®ã¢ãã¬ã¹ã¯ã以äžã®ããã«ããŠåºåããŸããå€æ°åã«&
ãã€ããããšã§ãã®ããŒã¿ã®å
é ã¢ãã¬ã¹ãåŸãããšãã§ããŸãããŸããprintln!
ã§ã¯{:p}
ãšããããšã§ãã€ã³ã¿ãŒã®åœ¢ã§æŽåœ¢ãåºåããããšãã§ããŸãã
fn main() {
let i1 = 1;
let i2 = 2;
println!("1: å€æ°i1ã®ã¹ã¿ãã¯äžã®ã¢ãã¬ã¹ : {:p}", &i1);
//=> 1: å€æ°iã®ã¹ã¿ãã¯äžã®ã¢ãã¬ã¹ : 0x94e9effa70
println!("2: å€æ°i2ã®ã¹ã¿ãã¯äžã®ã¢ãã¬ã¹ : {:p}", &i2);
//=> 1: å€æ°i2ã®ã¹ã¿ãã¯äžã®ã¢ãã¬ã¹ : 0x94e9effa74
}
å®éã«åºåãããå€ã¯ããã®æã
ã«ãã£ãŠå€ããããšã«æ³šæããŸããããŠãã¢ãã¬ã¹ã®æ«å°Ÿã«æ³šç®ãããšãi1
ã70
ãi2
ã74
ãšãªã£ãŠããŠã4ãã€ãã®ãããããããšãèªã¿åããŸããäžèšã®ã³ãŒãã§ååŸã§ããã¢ãã¬ã¹ã¯ãæ£ç¢ºã«ã¯ããŒã¿ã®å
é ã¢ãã¬ã¹ãæããŠããããããããå³è§£ãããšä»¥äžã®ããã«ãªã£ãŠãããšèããããŸãã
ç¶ããŠã¯f64
ãè©ŠããŸããf64
ã¯64bitãã€ãŸã8ãã€ãã§ããŒã¿ãè¡šçŸããŸããi32
ã®æã®ããã«ã¢ãã¬ã¹ãåºåãããšãã¢ãã¬ã¹ã«8ãã€ãã®ãããããããšãäºæ³ãããŸãã
fn main() {
// f64ãšæšè«ããã
let f1 = 1.0;
let f2 = 2.0;
println!("3: å€æ°f1ã®ã¹ã¿ãã¯äžã®ã¢ãã¬ã¹ : {:p}", &f1);
//=> 2: å€æ°f1ã®ã¹ã¿ãã¯äžã®ã¢ãã¬ã¹ : 0x72909bf4d8
println!("4: å€æ°f2ã®ã¹ã¿ãã¯äžã®ã¢ãã¬ã¹ : {:p}", &f2);
//=> 2: å€æ°f2ã®ã¹ã¿ãã¯äžã®ã¢ãã¬ã¹ : 0x72909bf4e0
}
ä»åã¯f
ã®æ«å°Ÿãd8
ãf2
ã®æ«å°Ÿãe0
ãšããçµæã«ãªããŸããã16é²æ°ãªã®ã§ã¡ãã£ãšåããã«ããã§ãããe0 - d8
ãèšç®ãããš10é²æ°ã§8
ã«ãªããŸãã
ç¶ããŠãé åãåãäžããŸããRustã«ãããŠé åã¯å€æŽäžå¯ã§ãããããããã³ã³ãã€ã«æã«ããŒã¿ãµã€ãºã決ãŸã£ãŠãããããããåèŠçŽ ãé åã«ç©ãŸããŠãããŸãã
fn main() {
// [i32; 3]ãšæšè«ããã
let arr = [1, 3, 5];
for (index, num) in arr.iter().enumerate() {
println!("é
åarrã®{}çªç®ã®å€ {}", index + 1 , num);
//=> é
åarrã®1çªç®ã®å€ 1
//=> é
åarrã®2çªç®ã®å€ 3
//=> é
åarrã®3çªç®ã®å€ 5
};
}
以äžã®ããã«ããŠãã¹ã¿ãã¯ã«ç©ãŸããŠããããŒã¿ã®ã¢ãã¬ã¹ãåŸãããšãã§ããŸãã
fn main() {
let arr = [1, 3, 5];
println!("é
åarrã®1çªç®ã®å€ã®ã¹ã¿ãã¯ã¢ãã¬ã¹ {:p}", &arr[0]);
println!("é
åarrã®2çªç®ã®å€ã®ã¹ã¿ãã¯ã¢ãã¬ã¹ {:p}", &arr[1]);
println!("é
åarrã®3çªç®ã®å€ã®ã¹ã¿ãã¯ã¢ãã¬ã¹ {:p}", &arr[2]);
//=> é
åarrã®1çªç®ã®å€ã®ã¹ã¿ãã¯ã¢ãã¬ã¹ 0x8d514ff814
//=> é
åarrã®2çªç®ã®å€ã®ã¹ã¿ãã¯ã¢ãã¬ã¹ 0x8d514ff818
//=> é
åarrã®3çªç®ã®å€ã®ã¹ã¿ãã¯ã¢ãã¬ã¹ 0x8d514ff81c
}
é
åã®åèŠçŽ ã¯i32
åãšæšè«ããã4ãã€ãã§è¡šçŸãããŸããç§ãå®è¡ãããšãæ«å°Ÿã14
ã18
ã1c
ãšãªããŸãããããããã®å·®ã¯4ã§ãã
ã¹ã¿ãã¯ã®åŸå ¥ãå åºããšããæ§è³ªäžããã©ãã«ããŒã¿ãç©ãããããã€ã©ã®ããŒã¿ãéæŸãããããšãã£ãããšãèããå¿ èŠã¯ãããŸããããã®æã ã®ãäžçªäžããè¡šãã¢ãã¬ã¹ãã©ããã«ä¿åããŠãããŠãããã«ç©ãã§ããããåãåºãã°ããã§ããããããé«éã«åŠçã§ãã人éãèããŠã¡ã¢ãªãŒã管çããå¿ èŠã¯ãããŸããã
ããŒãé å
察ããããŒãé åã«ã¯ãã¹ã¿ãã¯ã®æ§ãªé åºã ãŠãããèŠåã¯ãããŸããã以äžã®å³ã®ããã«ããã®æã«ç©ºããŠããå Žæãæ¢ãä¿åãã䜿ãçµãã£ãã解æŸããã€ã¡ãŒãžã§ãã
éã«èšããšãã©ã®ãããªããŒã¿ãã©ã®ãããªã¿ã€ãã³ã°ã§ã©ãã«ä¿åããããããããŠãã€è§£æŸãããããã¹ã¿ãã¯ã®ããã«æ©æ¢°çã«ã¯åãããŸããããã®ãããäœããã®ä»çµã¿ã䜿ã£ãŠãã®ããŒãé åã管çããå¿ èŠããããŸããããã¯ãšãŠãé£ãããã§ããã
C
Cã§ã¯ãããã°ã©ããŒãã³ãŒãäžã§malloc
ãfree
ãšãã£ãé¢æ°ãçšããŠããŒãé åã確ä¿/解æŸããŸãã
malloc
ã¯ãmemory allocationãã®ç¥ã§ããallocationã¯ãå²ãåœãŠããšãã£ãæå³ã§ããããèšèéãã¡ã¢ãªãŒã®å²ãåœãŠãè¡ãããšããé¢æ°ã§ããã
char *p;
// ãã€ã³ã¿ãŒå€æ°pã«ã¯ããŒãé åã®å
é ã¢ãã¬ã¹ãæ ŒçŽããã
p = malloc(20)
ãã®ãããã°ã©ããŒãããšãããšããããã€ã³ãã§ãé©åã«ã¡ã¢ãªãŒç®¡çãè¡ããªããšã¡ã¢ãªãŒã®å€ééæŸãäžæ£é åãžã®ã¢ã¯ã»ã¹ãªã©é倧ãªãšã©ãŒãçºçããŸãããèªç±ã«ã¯è²¬ä»»ã䌎ãããšã¯æ£ã«ãã®ããšã§ããã
äŸãã°ãAndroidã¯å€§éšåãC/C++ã§æžãããŠããããã§ãããã¡ã¢ãªãŒå®å šæ§ã«é¢ããåé¡ãè匱æ§ã®å€ããå ããŠããããã§ãã
ã¡ãªã¿ã«ãã¿ãã¬ãããšãRustã§ã¯malloc
ãfree
ãªã©ãçšããŠããŒãé åã管çããããšã¯åºæ¬çã«ã§ããŸããïŒunsafeãªåŠçãå¿
èŠïŒã
JavaScript
JavaScriptã§ã¯ã¬ããŒãžã³ã¬ã¯ã·ã§ã³ïŒä»¥äžãGCïŒãšããèšèªåŠçç³»ã®æ©æ§ãçšããŠãããŒãé åã管çããŸããäŸãšããŠJavaScriptãæããŸãããè¿ä»£ã®ã»ãšãã©ã®ããã°ã©ãã³ã°èšèªã«ãããå®è¡ç°å¢ã§ã¯ãGCãçšããŠããŒãé åã管çããŠãããšæãããŸãã
Cã®ããã«ãããã°ã©ããŒãæ瀺çã«ããŒãé åã確ä¿/解æŸãããšããåœä»€ãåºããªããŠããGCãOSãšååããŠã¡ã¢ãªãŒã管çããŠãããŸãã
ããããå šéšã®èšèªã§GCã«ãããããããããããšãªãããã§ããã
- ã»GCãèµ°ãåã®ã³ã¹ããããããã¢ããªã±ãŒã·ã§ã³ã®å®è¡é床ãèœã¡ãããåæ¢ããããšãããã
- ã»GCãèµ°ãã¿ã€ãã³ã°ã¯äººéãã³ã³ãããŒã«ã§ãããå¹çãé床ã®é¢ã§æè¯ãšã¯èšããªã
ãšãã£ãweak pointããããé«ãã¬ã€ã€ãŒã§åäœããã¢ããªã±ãŒã·ã§ã³ã§ã¯åé¡ãªããšããã¬ã€ã€ãŒãäœããªãã«äŒŽãåé¡ã«ãªãããšãå¢ããŠãããšæãããŸãã
ã¡ãªã¿ã«ãã¿ãã¬ãããšãRustã§ã¯GCãçšããŠããŒãé åã管çããããšã¯ã§ããŸããã
Rust
å è¿°ããéããRustã¯æææš©ãšããæ©æ§ã䜿ããã¡ã¢ãªãŒãªãœãŒã¹ã®ç¢ºä¿ãšè§£æŸãè¡ããŸããããã°ã©ããŒã¯æææš©ã«ã€ããŠæèããå¿ èŠã¯ãããŸãããããŒãé åã®ç¢ºä¿ãšè§£æŸãæ瀺çã«è¡ãå¿ èŠã¯ãããŸããã
æææš©
ã§ã¯æææš©ã«ã€ããŠãã³ãŒããæžããªãã確èªããŠãããŸããæææš©ãåžãä»çµã¿ãå éšã§ã©ããªé¢šã«åããŠãããã¯ç§ãå匷äžè¶³ã§åãããŸããããã³ãŒããæžããªãããã®æåãèŠãŠãããŸãããã
fn main() {
// 1. ããŒãé åã«"Hello Rust"ãšããæååãæ ŒçŽããã
// 1. å€æ°s1ã"Hello World"ã®ææè
ãšãªã
let s1 = String::from("Hello Rust");
} // 2. å€æ°s1ãã¹ã³ãŒãããå€ããããŒãé åã®"Hello Rust"ãç Žæ£ããã
æååãè¡šãStringåã®äŸã§ããå€Hello Rust
ãå€æ°s1
ã«æçžããããã®æHello Rust
ãããŒãé åã«æ ŒçŽãããŸããåæã«ãs1
ãHello Rust
ã®ææè
ãšãªããŸãã
æçµçã«s1
ãã¹ã³ãŒãããå€ããæç¹ã§ãææè
ã§ããs1
ã責任ãæã£ãŠããŒãé åã®Hello Rust
ãç Žæ£ããŸãããã®ä»çµã¿ãªãããŒã¿ããã£ãšããŒãé åã«æ®ãç¶ããããšã¯ãããŸãããã
ãããã人éãæ瀺çã«ãHello Rust
ãç Žæ£ããããšåœä»€ããŠããããã§ã¯ãªãããœãŒã¹ã³ãŒãã®æ§é ã«ãã£ãŠèšèªã¬ãã«ã§ãã®åŠçãè¡ãããŠãããšãããçŽ æµâšã§ãã
Stringå
ã§ã¯ããããæææš©ã«ã€ããŠæãäžããŠã¿ãŸããæåã«ãããŒã¿ãã©ã®ããã«ã¡ã¢ãªãŒã«æ ŒçŽãããŠãããã確èªããŠã¿ãŸãã
ããŒãé åã«ããŒã¿ãæ ŒçŽãããããŒã¿åãšããŠãå
ã»ã©ãšåããString
åãåãäžããŸãããã®åã®éããæååãè¡šãããŒã¿åã§ããã以äžã®ããã«å®çŸ©ããŸãã
fn main() {
let s1 = String::from("Hello World, I am Rustacean!");
let s2 = String::from("Hello Rust");
println!("s1ã®å€ {}", s1);
println!("s2ã®å€ {}", s2);
}
ããŠããã®s1
ãšs2
ãšããå€æ°ïŒStringæ§é äœïŒã¯ãããŒãé åã«ããå®ããŒã¿ãã®ãã®ã§ã¯ãªããå®ããŒã¿ãæããŠããåç
§ã®ãããªãã®ã§ãããã¹ã¿ãã¯ã«ç©ãŸããŠããŸãããããæ€èšŒããŠå³è§£ããŠã¿ãŸãã
ãŸãã¯ã以äžã®ããã«ããŠs1
ãšs2
ã®ã¢ãã¬ã¹ã確èªããŸãã
fn main() {
let s1 = String::from("Hello World, I am Rustacean!");
let s2 = String::from("Hello Rust");
println!("s1ã®ã¹ã¿ãã¯ã¢ãã¬ã¹ã®å€ {:p}", &s1);
//=> s1ã®ã¹ã¿ãã¯ã¢ãã¬ã¹ã®å€ 0x7ffc6be6e488
println!("s2ã®ã¹ã¿ãã¯ã¢ãã¬ã¹ã®å€ {:p}", &s2);
//=> s2ã®ã¹ã¿ãã¯ã¢ãã¬ã¹ã®å€ 0x7ffc6be6e4a0
}
ã¢ãã¬ã¹ã®æ«å°Ÿãa0
- 88
ãããŠã¿ããšã10é²æ°ã§24ã§ãããs1
ãs2
ã¯24ãã€ãã®å®¹éãæã£ãŠãããšæãããŸããs1
ãs2
ã¯ã¹ã¿ãã¯ã«ç©ãŸããŠããã®ã§ãå³è§£ãããšä»¥äžã®ããã«ãªããŸãã
ãã®24ãã€ãã®æ£äœã¯äœãªã®ã§ããããïŒ
çµè«ããèšããšããã®24ãã€ãã¯ãããŒãé åã«ããå®ããŒã¿ã®å
é ã¢ãã¬ã¹ãæããã€ã³ã¿ãŒïŒ8ãã€ãïŒããããŒã¿é·ïŒ8ãã€ãïŒããããŒãé åã«ç¢ºä¿ãããé åã®ãµã€ãºïŒ8ãã€ãïŒããšããå
èš³ã«ãªã£ãŠããŸããThe Bookã®äžã§ã¯ä»¥äžã®ããã«å³ç€ºãããŠããŸããå·Šãã¹ã¿ãã¯ã«ç©ãŸããŠãã24ãã€ããå³ãããŒãé åã«ããå®ããŒã¿ãè¡šçŸããŠããŸããæåã®8ãã€ãïŒptr
ïŒãããŒãé åã®å®ããŒã¿ã®å
é ã¢ãã¬ã¹ãæããŠããã®ãåãããšæããŸãã
ã§ã¯ãã®å³ãããå°ãåãããããæžãæããŠã¿ãŸããæåã®8ãã€ããã€ãŸãããŒãé åã«ããå®ããŒã¿ã®å
é ã¢ãã¬ã¹ãæããã€ã³ã¿ãŒã¯ã以äžã®ããã«as_ptr()
ã䜿ã£ãŠè¡šç€ºããŸãã
fn main() {
let s1 = String::from("Hello World, I am Rustacean!");
let s2 = "Hello Rust".to_string();
println!("s1ã®ã¹ã¿ãã¯ã¢ãã¬ã¹ã®å€ {:p}", &s1);
//=> s1ã®ã¹ã¿ãã¯ã¢ãã¬ã¹ã®å€ 0x7ffc6be6e488
println!("s1ã®ããŒãé åã®å®ããŒã¿ã®å
é ã¢ãã¬ã¹ {:p}", s1.as_ptr());
//=> s1ã®ã¹ã¿ãã¯ã¢ãã¬ã¹ã®å€ 0x7fff37d9be10
println!("s2ã®ããŒãé åã®å®ããŒã¿ã®å
é ã¢ãã¬ã¹ {:p}", s2.as_ptr());
//=> s2ã®ã¹ã¿ãã¯ã¢ãã¬ã¹ã®å€ 0x7fff37d9be60
}
ãããå³ç€ºãããšãããªããŸãïŒs1
ã®ã¿å³ç€ºããŸãïŒã
ç¶ããŠãããŒã¿é·ãæ ŒçŽããŠãã8ãã€ãã¯len()
ã§ååŸã§ããŸããs1ã¯ã¹ããŒã¹ãå
¥ããŠ28æåãs2ã¯ã¹ããŒã¹ãå
¥ããŠ10æåãªã®ã§ã以äžã®ãããªåºåã«ãªããŸãã
fn main() {
let s1 = String::from("Hello World, I am Rustacean!");
let s2 = String::from("Hello Rust");
println!("s1ã®å®ããŒã¿ã®ããŒã¿é· {}", s1.len());
//=> s1ã®å®ããŒã¿ã®ããŒã¿é· 28
println!("s2ã®å®ããŒã¿ã®ããŒã¿é·{}", &s2.len());
//=> s2ã®å®ããŒã¿ã®ããŒã¿é·10
}
ã¡ãªã¿ã«ãåºåãããã®ã¯ãããŸã§å®ããŒã¿ã®ããŒã¿é·ã§ãããæåæ°ãã«ãŠã³ãããŠããããã§ã¯ãããŸãããRustã¯æååãUTF-8ã§ãšã³ã³ãŒãããŠããŸããäŸãã°ããã«ã¡ã¯
ãšããæååãªãã1æå3ãã€ãã§ãã®ã§15
ãåºåãããŸãã
fn main() {
println!("ããã«ã¡ã¯ã®ããŒã¿é· {}", String::from("ããã«ã¡ã¯").len());
//=> ããã«ã¡ã¯ã®ããŒã¿é· 15
}
ã¡ãªã¿ã«ã®ã¡ãªã¿ã«ãchars()
ãçšããã°ä»¥äžã®ããã«ããã°æåæ°ãã«ãŠã³ãããããšãã§ããŸãã
fn main() {
println!("ããã«ã¡ã¯ã®æåæ° {}", String::from("ããã«ã¡ã¯").chars().count());
//=> ããã«ã¡ã¯ã®æåæ° 5
}
æåŸãããŒãé åäžã«ç¢ºä¿ãããé åã¯capacity()
ã§ååŸã§ããŸãã
fn main() {
let s1 = String::from("Hello World, I am Rustacean!");
let s2 = String::from("Hello Rust");
println!("s1ã®ããŒãé åã«ç¢ºä¿ãããé åã®ãµã€ãº {}", s1.capacity());
//=> s1ã®ããŒãé åã«ç¢ºä¿ãããé åã®ãµã€ãº 28
println!("s2ã®ããŒãé åã«ç¢ºä¿ãããé åã®ãµã€ãº {}", s2.capacity());
//=> s2ã®ããŒãé åã«ç¢ºä¿ãããé åã®ãµã€ãº 28
}
æååã®ãã€ãæ°ãå¢ããããããŠäœåºŠãè©ŠããŸããããããããlen()
ãšcapacity()
ã®å€ã¯åãã§ãããString
ã¯åŸããå€æŽå¯èœãªã®ã§ãå°ã倧ããã«capacity()
ãæã£ãŠããã®ããïŒãšçŽ 人çã«èããŠããŸããããããã§ããªãããã§ãã
ãšããããã§ãŸãšãããšãStringåã¯ä»¥äžã®ããã«ã¹ã¿ãã¯é åãšããŒãé åã«ãŸããã£ãŠããããŒã¿æ§é ã§ãã
s1
ãã¹ã³ãŒãããæããæã«ãs1
ãã¹ã¿ãã¯ããåãåºãããŸãããããŠãã®æãææè
ã§ããs1
ãããŒãé åã®ããŒã¿ã責任ãæã£ãŠåé€ããŸãã
æææš©ã¯ç§»åãã
èšäºã®åé ã§ãææè ã¯ããäžæç¹ã«ãããŠå¿ ãäžäººïŒã€ãŸãäžã€ã®å€æ°ïŒã§ããããšæžããŸããããããŸã§ã®ç¥èãå ã«æŽçãããšããããå€ãååšããŠãããã«å¯Ÿå¿ãããã£ãäžäººã®ææè ãååšããããã®ææè ãã¹ã³ãŒããæãããªã©ãããå€ãç Žæ£ããããå€ã®ç Žæ£ãå¿ããèµ·ããªãããææè ãäžäººã§ããããã¡ã¢ãªãŒã®äºééæŸãèµ·ããªãããšãã颚ã«èããããææè ãšå€ã¯äžè®æçã®éåœå ±åäœã®ããã«æããããšãã§ããŸãã
話ãããã§æžãã°ããã®ã§ããããããããã®æææš©ãšãããã®ã¯ä»ã®å€æ°ã«ç§»åããããšããããŸãããã®æææš©ã®ç§»åãmoveãšåŒã¶ããšã«ããŸãã
moveã¯ããã€ãã®å Žé¢ã§èµ·ãããŸããã以äžã®ããã«Stringåã®å€æ°ãä»ã®å€æ°ã«æçžããç¶æ³ãèããŠã¿ãŸãã
fn main() {
let s1 = String::from("Hello Rust");
// moveãèµ·ãã
let s2 = s1;
}
æææš©ã®ç§»åããšã¯æåéãã®ã移åãã§ãããã³ããŒãããããã§ã¯ãããŸãããäžèšã³ãŒãã¯åé¡ãªãã³ã³ãã€ã«ãéããŸããããã§ãæææš©ã倱ã£ãs1
ã«ã¢ã¯ã»ã¹ããããšãããšã³ã³ãã€ã«ã倱æããŸãã
fn main() {
let s1 = String::from("Hello Rust");
// moveãèµ·ãã
let s2 = s1;
println!("ðŠâ s1ã®å€ãåºåã§ãã?{}", s1);
}
ã³ã³ãã€ã«ãšã©ãŒãèµ·ãããšããšã©ãŒã¡ãã»ãŒãžãåºåãããŸããããããRustã®ãšã©ãŒã¡ãã»ãŒãžã¯æåããã»ã©äžå¯§ã§ãããã®äžå¯§ãããªããã°ãç§ã¯ãšã£ãã«Rustãè«ŠããŠããŸããã
//ãšã©ãŒã¡ãã»ãŒãž
error[E0382]: borrow of moved value: `s1`
--> src/main.rs:7:34
|
2 | let s1 = String::from("Hello Rust");
| -- move occurs because `s1` has type `String`, which does not implement the `Copy` trait
...
5 | let s2 = s1;
| -- value moved here
6 |
7 | println!("ðŠâ s1ã®å€ãåºåã§ãã?{}", s1);
| ^^ value borrowed here after move
|
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider cloning the value if the performance cost is acceptable
|
5 | let s2 = s1.clone();
| ++++++++
For more information about this error, try `rustc --explain E0382`.
borrow of moved values: s1
ãšããæèšããããŸããããs1
ã§moveãèµ·ãã£ãããborrowïŒåçšïŒã§ããªããããšããæå³ã§ããborrowãšããèšèã¯é£ããã§ããããæææš©ã倱ã£ãå€æ°ã«ã¯ã¢ã¯ã»ã¹ã§ããªãããšæããããšã«ããŸãã
ä»ã¯println!()
ã«s1
ãæž¡ããæçºã§ãšã©ãŒãèµ·ããŠããŸãããäŸãã°ä»ã®å€æ°ã«æçžãããªã©ã§ãåæ§ã®ãšã©ãŒãçºçããŸããæææš©ã倱ã£ãåŸs1
ã«ã¯ã©ããªåœ¢ã§ããã¢ã¯ã»ã¹ã§ããªãããšèããããšãã§ããŸãã
fn main() {
let s1 = String::from("Hello Rust");
// moveãèµ·ãã
let s2 = s1;
// ãã以éãs1ã«ã¯ã¢ã¯ã»ã¹ã§ããªã
let s3 = s1;
// åæ§ã®ãšã©ãŒ
}
ããŠãHello Rust
ãšããå®äœã®æææš©ã¯s2
ã«ç§»ã£ãŠããŸããã§ãã®ã§s2
ã«ã¯åé¡ãªãã¢ã¯ã»ã¹ã§ããŸãã
fn main() {
let s1 = String::from("Hello Rust");
// moveãèµ·ãã
let s2 = s1;
println!("s2ã®å€ãåºåã§ãã {}", s2);
// Hello Rust
}
ãã®moveã®åäœãããç解ãããããã¡ã¢ãªãŒã¢ãã¬ã¹ãåºåããªããå床åäœç¢ºèªããŠã¿ãŸããs1
ã«å€ãæçžããåŸãs1.as_ptr
ã§ããŒãé åã®Hello Rust
ãä¿åãããŠããã¢ãã¬ã¹ãåºåããŸããs2
ã«æææš©ã移ã£ãåŸãs2.as_ptr()
ãšãã颚ã«å床å®äœã®ã¢ãã¬ã¹ãåºåããŠã¿ãŸãããšãã«åãã¢ãã¬ã¹ãåãåºãã¯ãã§ãã
fn main() {
let s1 = String::from("Hello Rust");
println!("s1ã®as_ptrã®å€ {:p}", s1.as_ptr());
//=> s1ã®as_ptrã®å€ 0x7fff37d9be10
let s2 = s1;
println!("s2ã®as_ptrã®å€ {:p}", s2.as_ptr());
//=> s2ã®as_ptrã®å€ 0x7fff37d9be10
// s1ãšåã
}
ããã¯å¡ãã以äžã®ããã«ã€ã¡ãŒãžã§ããŸãã
ãããŠãs2
ãã¹ã³ãŒãããæããæãããŒã¿ã®ææè
ãšããŠè²¬ä»»ãæã£ãŠHello Rust
ãç Žæ£ããŸãã
æææš©ã®ç§»åãçºçããã±ãŒã¹
ããŠãæææš©ã移åããã±ãŒã¹ã¯æ§ã ãããŸãããç¶ããŠé¢æ°ã®å€ãæž¡ãæã«ã€ããŠãã³ãŒããæžããªããèŠãŠãããŸãã
åãåã£ãString
ãåºåããã ãã®echo
é¢æ°ãäœæããŸãã
fn echo(s: String) {
println!("{}", String);
}
main
é¢æ°ã®äžã§String
åã®ããŒã¿s1
ãäœæããecho
é¢æ°ã«æž¡ããŸããecho
é¢æ°èªäœã¯ã¡ãããšå®è¡ãããŸãããæž¡ããåŸãmain
é¢æ°ã§s1
ã«ã¢ã¯ã»ã¹ã§ããã§ããããïŒ
fn echo(s: String) {
println!("{}", s);
//=> Hello Rust
}
fn main() {
let s1 = String::from("Hello Rust");
// s1ãæž¡ã
echo(s1);
println!("ðŠâ s1ã«ã¢ã¯ã»ã¹ã§ããïŒ {}", s1);
}
ã¯ããã§ããŸããð¥ãã³ã³ãã€ã«ãšã©ãŒã«ãªããŸãããšã©ãŒã¡ãã»ãŒãžã泚ææ·±ãèŠãŠã¿ãŸãããã
Compiling playground v0.0.1 (/playground)
error[E0382]: borrow of moved value: `s1`
--> src/main.rs:11:35
|
7 | let s1 = String::from("Hello Rust");
| -- move occurs because `s1` has type `String`, which does not implement the `Copy` trait
8 |
9 | echo(s1);
| -- value moved here
10 |
11 | println!("ðŠâ s1ã«ã¢ã¯ã»ã¹ã§ããïŒ {}", s1);
| ^^ value borrowed here after move
|
note: consider changing this parameter type in function `echo` to borrow instead if owning the value isn't necessary
--> src/main.rs:1:12
|
1 | fn echo(s: String) {
| ---- ^^^^^^ this parameter takes ownership of the value
| |
| in this function
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider cloning the value if the performance cost is acceptable
|
9 | echo(s1.clone());
| ++++++++
echo(s1);
ã®æã§-- value moved here
ãšèšè¿°ãããŠããŸãããecho
é¢æ°ã®ä»®åŒæ°s
ã«æææš©ã移åããŠããŸã£ããããmain
é¢æ°ã®äžã§ã¯ããs1
ã«ã¢ã¯ã»ã¹ããããšã¯åºæ¥ãªããªã£ãŠããŸããŸãããecho
é¢æ°ã®å€æ°s
ãã¹ã¿ãã¯ããåãåºããããšããããŒãé åã®ããŒã¿ãåé€ãããããšã«ãªããŸãã
ä»ã®å€æ°ãžã®æçžããããŠé¢æ°ãžæž¡ãããšãã2ãã¿ãŒã³ã®æææš©ã®ç§»åãèŠãŠããŸãããæææš©ã移åããããšã¯ãã£ãŠãã決ããŠéè€ããããšã¯ãããŸãããã€ãŸã誰ãããŒã¿ãç Žæ£ããããæ確ã«æ±ºãŸã£ãŠããã®ã§ãããŒã¿ãæ®ãç¶ãããšããããšã¯èµ·ããããªãããšãããããŸãã
ã³ããŒãã¬ã€ã
ãããŸã§ã¯Stringåã®ããŒã¿ãäŸã«ãšã£ãŠããŸããããä»ã®åã¯ã©ããªã£ãŠããã®ã§ããããïŒäŸãã°ãããªããã£ãåãšããããi32åãf64åãªã©ã§ãã
å è¿°ãããšããããããã¯ããŒã¿ãã¹ã¿ãã¯ã«ç©ãŸããŸãããããŠããå€æ°ãä»ã®å€æ°ã«æçžããæãæææš©ã®ç§»åã¯èµ·ããããå€ãã³ããŒãããŸãã
fn main() {
let i1 = 10;
// å€ã®ã³ããŒãèµ·ãã
let i2 = i;
println!("i3ãši4ã«åæã«ã¢ã¯ã»ã¹ã§ãã i3 = {}, i4 = {}", i3, i4);
//=> i3ãši4ã«åæã«ã¢ã¯ã»ã¹ã§ãã i3 = 10, i4 = 10
}
ãã®ãå€ãã³ããŒãããåããã³ããŒã»ãã³ãã£ã¯ã¹ãšåŒã³ãŸãïŒå¯ŸããŠãæææš©ã移åããåãã¯ã ãŒãã»ãã³ãã£ã¯ã¹ãšãåŒã³ãŸãïŒã
ããåã«ã€ããŠãã©ã¡ãã®ã»ãã³ãã£ã¯ã¹ã®åããããã®ããã©ããã£ãŠèããã°ããã®ã§ãããããããªããã£ãåã¯ã³ããŒã»ãã³ãã£ã¯ã¹ãè€ååã¯ã ãŒãã»ãã³ãã£ã¯ã¹ããšå€§éæã«èããŠã倧äœã¯ããã£ãŠããã®ã§ãããä»æ§æžãå ã«æ£ç¢ºãªããšã調ã¹ãŠã¿ãŸãã
ã³ããŒã»ãã³ãã£ã¯ã¹ãèµ·ããåã¯ãCopyãã¬ã€ããå®è£
ãããŠããŸããCopyãã¬ã€ãã®ããŒãžã®Implementorsã®ã»ã¯ã·ã§ã³ã«ãCopyãã¬ã€ããã©ã®åã«å®è£
ãããŠããããæ²èŒãããŠããŸããäŸãã°ããã«ãimpl Copy for i32
ãšãããŸããã察ããŠãStringãVecã®æåã¯èŠåœãããŸããã
éã«ãStringåãã©ã®ãã¬ã€ããå®è£ ããŠãããã¯Stringåã®ããŒãžã®Implementationsã®ã»ã¯ã·ã§ã³ã§ç¢ºèªã§ããŸããããã«Copyãã¬ã€ããå®è£ ãããŠããæ§åã¯èŠãããŸããã
åçš
ããŠãããã¯ãšãŠãäžäŸ¿ã«æãããããããŸããð€®ãå®å šãªã®ã¯åãããã§ãããããå°ãèéããããªãã®ã§ããããïŒ
Rustã«ã¯åçšãšããä»çµã¿ãçšæãããŠããŠããããäžæã«çšããããšã§äžèšã®çª®å±ããã解æŸãããã³ãŒããæžãããšãã§ããŸãã
ä»ã®å€æ°ã«å€ãæçžããæã®ããšãèããŠã¿ãŸããæçžãè¡ãæã&s1
ãšãã颚ã«èšè¿°ããã°ãmoveã¯èµ·ãããŸãããs1
ã«ãs2
ã«ãåæã«ã¢ã¯ã»ã¹ã§ããŠããããšãåãããŸãã
fn main() {
let s1 = String::from("Hello Rust");
// &ãä»ãã
let s2 = &s1;
println!("{}, {}", s1, s2);
//=> Hello Rust, Hello Rust
}
ãã®åçšãšããä»çµã¿ã¯ã©ã®ããã«æããã°ããã®ã§ããããïŒs1ãs2ã¯äŸã«Stringæ§é äœã®ã€ã³ã¹ã¿ã³ã¹ã§ãã®ã§ãas_ptr()
ã§å®äœãæ ŒçŽãããŠããã¢ãã¬ã¹ãåºåããããšãã§ããŸãã
fn main() {
let s1 = String::from("Hello Rust");
let s2 = &s1;
println!("s1ã®å®äœã®ã¢ãã¬ã¹{:p}", s1.as_ptr());
//=> s1ã®å®äœã®ã¢ãã¬ã¹0x562d7a5069d0
println!("s2ã®å®äœã®ã¢ãã¬ã¹{:p}", s2.as_ptr());
//=> s2ã®å®äœã®ã¢ãã¬ã¹0x562d7a5069d0
}
äžèšã®æ§ã«å®è¡ããã°ãåãã¢ãã¬ã¹ãåºåãããã¯ãã§ãã2ã€ã®å€æ°ã¯ããŒãé åäžã®åãå Žæãåç §ããŠãããšããããšã§ããã
ãã£ãšè©³ããèšããšãs2ãšããå€æ°ã¯s1ãæããŠããŠs1ã¯ããŒãé åãæããŠãããšããããšã§ããThe bookã§ã¯ãåç §ã«ã€ããŠã以äžã®ããã«å³ç€ºããŠããŸãã
ãããåœããã°é¢šã«ç€ºããšãããã以äžã®ããã«ãªããŸãã
ãã®ããã«ã&
ãã€ããŠïŒæææš©ã®ç§»åã䌎ããïŒå€ãåç
§ããããšãå
񑆇
§ãšèšããŸãã
ãã®å
񑆇
§ã®ä»çµã¿ã䜿ã£ãŠãå
è¿°ããecho
é¢æ°ãæžãæããŠã¿ãŸãã
echoé¢æ°ã®ä»®åŒæ°ã¯&String
ãšãã颚ã«&
ãã€ããŠèšè¿°ããŸãããããŠecho
é¢æ°ã«æž¡ãæãecho(&s1)
ãšãã颚ã«åç
§ã®åœ¢ã§æž¡ããŸãããããªãecho
é¢æ°ã®å€æ°s
ã«æææš©ã¯ç§»åãããmain
é¢æ°ã®äžã§åŒãç¶ãs1
ã«ã¢ã¯ã»ã¹ã§ããŸãã
// &ãä»ããŠåå®çŸ©ãã
fn echo(s: &String) {
println!("{}", s);
//=> Hello Rust
}
fn main() {
let s1 = String::from("Hello Rust");
// s1ãå
񑆇
§ã®åœ¢ã§æž¡ã
echo(&s1);
// æææš©ã¯ç§»åããŠããªãã®ã§ãs1ã«ã¢ã¯ã»ã¹ã§ãã
println!("{}", s1);
//=> Hello Rust
}
å ±æåç §ã¯è€æ°åæã«ååšã§ãã
å
񑆇
§ã¯è€æ°åæã«ååšã§ããŸãã以äžã®äŸã§ã¯s1
ã®å
񑆇
§ã3ã€åæã«ååšããŠããŸãã
fn main() {
let s1 = String::from("Hello World");
// å
񑆇
§ã3ã€äœã
let s2 = &s1;
let s3 = &s1;
let s4 = &s1;
// 4ã€ã®å€æ°ã«åæã«ã¢ã¯ã»ã¹ã§ãã
println!("{}, {}, {}, {}", s1, s2, s3, s4);
//=> Hello World, Hello World, Hello World, Hello World
}
ãŸããå
񑆇
§ã®å
񑆇
§ãäœæã§ããŸãã以äžã®äŸã§ã¯s3
ãs2
ãåç
§ããs2
ãs1
ãåç
§ããããŠs1
ãããŒãé åã®å®ããŒã¿ãåç
§ããŠããããšãã圢ã«ãªã£ãŠããŸãã
fn main() {
let s1 = String::from("Hello World");
let s2 = &s1;
let s3 = &s2;
println!("{}, {}, {}", s1, s2, s3);
}
ããã§æ³šç®ãã¹ããã€ã³ãã¯ãããŒãé åã®å®äœãåç §ããŠããå€æ°ãããã€ã§ããŠããææè ã¯äžäººã ããšããã«ãŒã«ã¯å®ãããŠããããšããç¹ã§ãã
s1
ã«ãs2
ã«ãåæã«ã¢ã¯ã»ã¹ã§ããŠããããšãããããŸãããã®æãã¹ã¿ãã¯é åãšããŒãé åã¯ã©ããªã£ãŠããã®ã§ããããïŒ
Copyãã¬ã€ããå®è£ ããŠããåã¯æææš©ã®ç§»åã§ã¯ãªããå€ã®ã³ããŒãè¡ãããŸããå ·äœçã«ã¯æŽæ°åãæµ®åå°æ°ç¹åãçåœå€åãæååãèŠçŽ ãå šãŠCopyãã¬ã€ããå®è£ ããŠããã¿ãã«ããªã©ã
s2
ã¯s1
ãæããŠãããã ãããs1
ãå
ã«ããããããããšs2
ã¯ã©ããæããŠããã®ãåãããªããªããŸãããïŒ
ç»åãæ¿å ¥
ããã¯ã©ã€ãã¿ã€ã ãšåŒã°ããèŠçŽ ã絡ãã§ããŸãã
fn concat(a: String, b: String) -> String {
let c = format!("{}, {}", a, b);
c
}
pub fn main() {
let s1 = String::from("Hello");
let s2 = String::from("Rust");
// s1ãšs2ã®æææš©ã¯é¢æ°concatã®aãšbã«ç§»åãã
let s = concat(s1, s2);
println!("{}", s1);
println!("{}", s2);
}
Copyãã¬ã€ããå®è£ ããŠããåã¯æææš©ã®ç§»åã§ã¯ãªããå€ã®ã³ããŒãè¡ãããŸããå ·äœçã«ã¯æŽæ°åãæµ®åå°æ°ç¹åãçåœå€åãæååãèŠçŽ ãå šãŠCopyãã¬ã€ããå®è£ ããŠããã¿ãã«ããªã©ã
s2
ã¯s1
ãæããŠãããã ãããs1
ãå
ã«ããããããããšs2
ã¯ã©ããæããŠããã®ãåãããªããªããŸãããïŒ
ç»åãæ¿å ¥
ããã¯ã©ã€ãã¿ã€ã ãšåŒã°ããèŠçŽ ã絡ãã§ããŸãã
copy()
äœè«ã§ãããã¿ããªå€§å¥œãJavaScriptãšRustã§åäœãæ¯ã¹ãŠã¿ãããšæããŸãã
äžèšã®Rustã®ã³ãŒãäŸãšå€§äœåããããªããšãããŠããŸãããobj
ãobj2
ãã¢ã¯ã»ã¹ã§ããŸãããã€ãŸããåããªããžã§ã¯ãã2人ãåæã«æããŠãããšããããšã§ããRustè³ð§ ã§èãããšã誰ãçã¥ããã®ïŒããšãªã£ãŠããŸããŸãããçã¥ããã®ã¯JavaScriptïŒV8ãªã©ïŒã®GCã§ãïŒå°ãäžã§è§ŠããŸãããïŒã
代å
¥ãæçžãè¡ã=
ãšããæŒç®åã¯ãšãŠãåçŽãªããã«èŠããŸãããã¡ã¢ãªãŒã¬ãã«ãŸã§èãããšèšèªã«ãã£ãŠçµæ§éããããããšãããããŸãã
å¯å€åç §
å¯å€åç
§ã¯ãmoveãªãã«åç
§å
ã®å€ãæžãæããããšãèšããŸãã&mut
ãã€ããããšã§ãå€ã®æžãæããå¯èœã«ãªããŸãã
äŸãšããŠãæž¡ãããStringåã®ããŒã¿ã®æ«å°Ÿã«!!!
ãä»äžãããadd_exclamation
é¢æ°ãèããŸãã
fn add_exclamation(mut s: String) {
s.push_str("!!!");
println!("{}", s);
//=> Hello Rust!!!
}
fn main() {
let mut s = String::from("Hello Rust");
add_exclamation(s);
}
ãã ãããã ãšadd_exclamation
é¢æ°ãžã®moveãèµ·ãããé¢æ°ãåŒã³åºãã以éãmain
é¢æ°ã§s
ã¯äœ¿ããªããªããŸãã
fn add_exclamation(mut s: String) {
s.push_str("!!!");
println!("{}", s);
//=> Hello Rust!!!
}
fn main() {
let mut s = String::from("Hello Rust");
add_exclamation(s);
println!("ðŠâ sã«ã¯ã¢ã¯ã»ã¹ã§ããªã {}", s);
/*
error[E0382]: borrow of moved value: `s`
--> src/main.rs:13:35
|
9 | let mut s = String::from("Hello Rust");
| ----- move occurs because `s` has type `String`, which does not implement the `Copy` trait
10 |
11 | add_exclamation(s);
| - value moved here
12 |
13 | println!("ðŠâ sã«ã¯ã¢ã¯ã»ã¹ã§ããªã {}", s);
| ^ value borrowed here after move
|
}
*/
}
ãããªæã«ã¯å¯å€åç
§ã䜿ããŸããä»®åŒæ°ã®å®çŸ©ã§&mut String
ãšããé¢æ°ã«æž¡ããšãã«ã&mut s
ãšããŸãã
fn add_exclamation(s: &mut String) {
s.push_str("!!!");
println!("{}", s);
//=> Hello Rust!!!
}
fn main() {
let mut s = String::from("Hello Rust");
add_exclamation(&mut s);
println!("sã«ã¢ã¯ã»ã¹ã§ãã {}", s);
//=> sã«ã¢ã¯ã»ã¹ã§ãã Hello Rust!!!
}
ãªããå¯å€åç
§ã䜿ãããšããadd_exclamation
é¢æ°ã§å å·¥ããæååãè¿ããŠãmainé¢æ°
ã§åãåãããšããããšãã§ããŸãããå¯å€åç
§ã䜿ã£ãæ¹ãã³ãŒãã¯èŠããããšæããŸãã
æååã®ããŒã¿æ§é
Rustã«ãããŠãæååãè¡šãããŒã¿åã¯ãå
è¿°ããString
åãšãæååã¹ã©ã€ã¹åããããŸãïŒåã§ããæ¬åœã¯ãã£ãšãããŸãïŒã
æååã¹ã©ã€ã¹åã¯ã以äžã®ã³ãŒãäŸã®ããã«ãæååãããã«ã¯ãªããŒã·ã§ã³ã§å²ã£ãŠèšè¿°ãããšçæãããŸãã
fn main() {
let s1 = "hello world";
}
ãã®æååã¹ã©ã€ã¹åãšStringåã¯æ確ã«åºå¥ãããŸããäŸãã°ã&stråã®åŒæ°ãæã€é¢æ°ã«Stringåã®å€ãæž¡ããŠãã³ã³ãã€ã«ã§ããŸããã&
ãä»ããŠåç
§ããããšã§æž¡ãããšãã§ããŸãã
fn func(str: &str) {
println!("{}", str);
}
fn main() {
let s1 = "hello world";
// OK
func(s1);
// ðŠâ Stringåã¯æž¡ããªã
// func(String::from("hello world"));
//=> expected &str, found String
// OKãåç
§ããããšã§æååã¹ã©ã€ã¹ãšããŠæž¡ãã
func(&String::from("hello world"));
}
æååã¹ã©ã€ã¹ã¯ãã°ãã°&str
ãšè¡šèšãããŸãã&
ãã€ããŠããéãåç
§åã§ãããããããŒã¿å€æŽã¯ã§ããŸããã
æååãªãã©ã«ã§çæãããæååã¹ã©ã€ã¹ã®ããŒã¿ã¯ãã³ã³ãã€ã«æã«éçé åã«çœ®ãããããã°ã©ã ã®å®è¡æã«ã¡ã¢ãªãŒã«ããŒããããŸãã
æææš©ã«ã€ããŠå匷ããå 容ããã£ãšæžããŠã¿ãŸãããããã¯ããŸã åãã£ãŠããªãæããããçšèªãæ£ç¢ºã§ãªãã£ãããŒããããŠãããæšå¯ãå ã«æžããŠããéšåããããŸãã
ããã¡ããæžãããããšããã£ãã®ã§ãããããå°ãå匷ããåŸã«ç¶ããšããŠèšäºã«ãããããããªããããã®èšäºãå€§å¹ ã«æžãçŽããããããŸããã
åè
æææš©ãšã¯ïŒ - The Rust Programming Language æ¥æ¬èªç
Rust ã¯äœã解決ããããšããã®ãïŒã¡ã¢ãªãšãªãœãŒã¹ãšæææš©
ãããïŒïŒRustã®æææš©ã·ã¹ãã
Rustã®ã¡ã¢ãªç®¡çæ©èœãšãã®ç¹åŸŽ | å·±ã®äžåŠãæ¥ãã
Memory management - JavaScript | MDN
ããŒãå²ãåœãŠ | Writing an OS in Rust
JavaScriptããã©ãŠã¶ã§ã©ã®ããã«åãã®ã | ã¡ã«ã«ãªãšã³ãžãã¢ãªã³ã°
Rust ã®ã¡ã¢ãªç®¡ç | OKAZAKI Shogoâs Website
Rust ã®å | OKAZAKI Shogoâs Website
ãã³ã°ãªã³ã°ãã€ã³ã¿ãšã¯ïœdangling pointerã®å±éºæ§ãšåé¿ | MaryCore
ã¹ã¿ãã¯ãšããŒããç¥ã
ã¡ã¢ãªãšã¹ã¿ãã¯ãšããŒããšããã°ã©ãã³ã°èšèª | κeenã®Happy Hacκing Blog
Rustã®æææš©ïŒownershipïŒãèªçŸ©ããç解ãã - igagurimk2ã®æ¥èš
Rustã®å¯å€åç §ã®æåããããã«ãã - Qiita
What are the differences between Rustâs String
and str
? - Stack Overflow