Last active
March 31, 2024 05:09
-
-
Save keisuke-umezawa/1f5e418aff34e9e4309a0ba4f5918a50 to your computer and use it in GitHub Desktop.
Rust vs C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int my_account = 0; // 私の預金口座残高 | |
int your_account = 100; // あなたの預金口座残高 | |
// 送金処理: データ競合(data race)あり! | |
bool racy_transfer(int& src, int& dst, int m) | |
{ | |
if (m <= src) { // 未定義動作の可能性あり! | |
src -= m; // 未定義動作の可能性あり! | |
dst += m; // 未定義動作の可能性あり! | |
return true; | |
} else { | |
return false; | |
} | |
} | |
// 下記の2操作を別スレッドから同時に呼び出し | |
racy_transfer(your_account, my_account, 50); // [A] | |
racy_transfer(your_account, my_account, 80); // [B] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn f1(a: &String, b: &String) {} | |
fn f2(a: &mut String, b: &String) {} | |
fn f3(a: &mut String) {} | |
fn main() { | |
let mut a = "hoge".to_string(); // 可変変数の宣言 | |
f1(&a, &a); // 不変参照のみ借用しているので OK | |
f2(&mut a, &a); // コンパイルエラー! | |
f3(&mut a); // 可変参照を 1 つだけ借用しているので OK | |
println!("{}", a); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int main() { | |
std::string* x; | |
{ | |
std::string y = "y"; | |
x = &y; | |
} // yのスコープが終わり、ここで"y"の値は開放されてしまう。 | |
std::cout << *x << std::endl; // アクセス違反 | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
let x; | |
{ | |
let y = "hoge".to_string(); | |
x = &y; // コンパイルエラー! | |
} | |
println!("{}", x); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
let mut x = "x".to_string(); // 文字列 "x" を変数xにbindする | |
let y = "y".to_string(); // 文字列"y"を変数yにbindする | |
println!("{}", x); // x | |
println!("{}", y); // y | |
x = y; // C++のmoveと同じように、変数yにある文字列の所有権は変数yに移譲される | |
println!("{}", x); // y | |
// println!("{}", y); // コメントをはずすとコンパイルエラー | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int main(int argc, char** argv) { | |
auto x = std::make_unique<std::string>("x"); | |
auto y = std::make_unique<std::string>("y"); | |
std::cout << *x << std::endl; // x | |
std::cout << *y << std::endl; // y | |
x = std::move(y); // moveによってyが持つポインタの所有権をyに移譲する | |
std::cout << *x << std::endl; // y | |
std::cout << *y << std::endl; // メモリアクセス違反 | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment