Skip to content

Instantly share code, notes, and snippets.

@akehoyayoi
Last active September 5, 2018 08:42
Show Gist options
  • Save akehoyayoi/90cfa245f62f8e5d89ba8d71b369e26d to your computer and use it in GitHub Desktop.
Save akehoyayoi/90cfa245f62f8e5d89ba8d71b369e26d to your computer and use it in GitHub Desktop.
[Rust]所有権、所有権、所有けんけんけけんけん ref: https://qiita.com/akehoyayoi/items/abe58c8d7be198587a34
#include <stdio.h>
#include <string>
#include <iostream>
#include <vector>
int main(){
auto s = new std::vector<std::string>();s->push_back("udon");s->push_back("ramen");s->push_back("soba");
auto t = s; s = NULL;
auto u = s; s = NULL;
std::cout << (*s)[0] << std::endl;
return 0;
}
#include <stdio.h>
#include <string>
#include <iostream>
#include <vector>
int main(){
auto s = new std::vector<std::string>();s->push_back("udon");s->push_back("ramen");s->push_back("soba");
auto t = s; s = NULL;
auto u = s; s = NULL;
std::cout << (*s)[0] << std::endl;
return 0;
}
fn main() {
let s = vec!["udon".to_string(),"ramen".to_string(),"soba".to_string()];
let t = s; // sはここで所有権を失う
let u = t;
println!("s[0] is: {}", s[0]); // 所有権のないsにアクセス!
}
$ g++ -std=c++11 main.cpp
$ ./a.out
Segmentation fault: 11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment