Skip to content

Commit 76d6d83

Browse files
committed
more examples
1 parent 1797d87 commit 76d6d83

File tree

27 files changed

+247
-0
lines changed

27 files changed

+247
-0
lines changed

mut/iterator-invalidation/Cargo.lock

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mut/iterator-invalidation/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[package]
2+
name = "mutability"
3+
version = "0.1.0"
4+
authors = ["A new Rust developer <[email protected]>"]
5+
6+
[dependencies]

mut/iterator-invalidation/src/main.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
fn main() {
2+
let mut list = vec![1, 2, 3];
3+
for i in &list {
4+
println!("i is {}", i);
5+
list.push(i + 1);
6+
}
7+
}

mut/mut-example/Cargo.lock

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mut/mut-example/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[package]
2+
name = "mutability"
3+
version = "0.1.0"
4+
authors = ["A new Rust developer <[email protected]>"]
5+
6+
[dependencies]

mut/mut-example/src/main.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#[derive(Debug)]
2+
struct Account {
3+
balance: u32,
4+
}
5+
6+
impl Account {
7+
8+
// earn money
9+
fn earn(&mut self, amount: u32) {
10+
self.balance += amount;
11+
}
12+
13+
}
14+
15+
fn transfer(source: &mut Account, target: &mut Account, amount: u32) {
16+
source.balance -= amount;
17+
target.balance += amount;
18+
}
19+
20+
fn main() {
21+
let mut acct1 = Account { balance: 20 };
22+
let mut acct2 = Account { balance: 10 };
23+
24+
transfer(&mut acct1, &mut acct2, 3);
25+
26+
let mut acct3 = Account { balance : 0 };
27+
acct3.earn( 10);
28+
29+
println!("Account 1: {:?}", acct1);
30+
println!("Account 2: {:?}", acct2);
31+
println!("Account 3: {:?}", acct3);
32+
}

ownership-pluralize/0-starting-exercise/Cargo.lock

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[package]
2+
name = "ownership"
3+
version = "0.1.0"
4+
authors = ["A new Rust developer <[email protected]>"]
5+
6+
[dependencies]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
fn main() {
2+
let s = String::from("item");
3+
4+
// Add code here that calls the pluralize function
5+
6+
println!(
7+
"I have one {}, you have two {}",
8+
s,
9+
you_add_something_here,
10+
);
11+
}
12+
13+
// Add appropriate parameters, return values, and implementation to this function
14+
fn pluralize() {}

ownership-pluralize/1-working-solution/Cargo.lock

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)