Skip to content

Commit fb07ef2

Browse files
committed
Add README and a few comments
1 parent ace7300 commit fb07ef2

File tree

7 files changed

+26
-11
lines changed

7 files changed

+26
-11
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Intro to Rust
2+
3+
These are the code examples I used in the March 9 talk at OSC. They are intended to show how the Rust compiler rejects memory-related bugs that are typically uncaught in other languages. Here is a breakdown of the examples:
4+
5+
* `lifetime.cpp` demonstrates a perfectly valid C++ program that uses an invalid pointer. `lifetime.rs` is the equivalent Rust code that is rejected by the compiler.
6+
* `ownership.cpp` demonstrates a perfectly valid C++ program that makes use of deallocated memory. `ownership.rs` is the equivalent Rust code that is rejected by the compiler.
7+
* `concurrency.cpp` demonstrates a perfectly valid C++ program that contains a data race. `concurrency.rs` is the equivalent Rust code that is rejected by the compiler. A valid version of the Rust code is commented out in `concurrency.rs`
8+
9+
Note that the examples are necessarily contrived.

concurrency.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ int main() {
99
int acc = 0;
1010

1111
vector<thread> threads;
12+
// create 10 threads
1213
for (int i = 0; i < 10; i++) {
1314
threads.push_back(thread([&]() {
1415
for (int j = 0; j < 100000; j++) {
15-
acc++;
16+
acc++; // data race happens here; acc is not synchronized
1617
}
1718
}));
1819
}

concurrency.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ fn main() {
1818
println!("{}", acc);
1919
}
2020

21+
// Following is the correct, valid code
2122

2223
// use std::thread;
2324
// use std::sync::{Arc,Mutex};

lifetime.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ using namespace std;
66
int main() {
77
string *ptr;
88
{
9-
string s;
9+
string s; // s is allocated here
1010
s = "Hello world!";
1111
cout << s << endl;
1212
ptr = &s;
13+
// s is destroyed here
1314
}
1415
cout << *ptr << endl;
1516
}

lifetime.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
fn main() {
22
let ptr = {
3-
let s = "Hello world!".to_string();
3+
let s = "Hello world!".to_string(); // lifetime of s starts here
44
println!("{}", s);
55
&s
6+
// lifetime of s ends here
67
};
7-
println!("{}", ptr);
8+
println!("{}", *ptr); // &s is used here
89
}

ownership.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
#include <vector>
33
#include <string>
44

5+
using namespace std;
6+
57
int main() {
6-
std::vector<std::string> v;
8+
vector<string> v; // the first owner
79
v.push_back("Hello");
8-
std::string& x = v[0];
9-
v.push_back("world");
10-
std::cout << x;
10+
string& x = v[0]; // the second owner
11+
v.push_back("world"); // the first owner frees the underlying memory
12+
cout << x; // the second owner uses the freed (invalid) memory
1113
}

ownership.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
fn main() {
2-
let mut v = vec![];
2+
let mut v = vec![]; // v owns the vector
33
v.push("Hello");
4-
let x = &v[0];
5-
v.push("world");
4+
let x = &v[0]; // x borrows the vector
5+
v.push("world"); // v tries to modify the vector
66
println!("{}", x);
77
}

0 commit comments

Comments
 (0)