Skip to content

Commit c9cb2ec

Browse files
committed
Added other cases
1 parent e259606 commit c9cb2ec

8 files changed

Lines changed: 159 additions & 1 deletion

File tree

Lesson_04/src/case_04.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
pub fn run() {
2+
/*
3+
run scope'u içindeki average ile calculate fonksiyonuna koplayanarak taşınan
4+
versiyonu arasında bir ilişki yoktur. Dolayısıyla average içeriği calculate fonksiyonuna
5+
alınıp değiştirilse bile run içerisindeki değeri değişmez.
6+
*/
7+
let average = 3.14;
8+
println!("Average: {}", average);
9+
calculate(average);
10+
println!("After calculate, {}", average);
11+
12+
// Gönderilen değerin değiştirilip yeni bir değer olarak geri alınması yöntemi
13+
let new_average = increase_one(average);
14+
println!("After increase, {}", new_average);
15+
}
16+
17+
fn calculate(input: f64) {
18+
let _ = input * 0.1;
19+
}
20+
21+
fn increase_one(input: f64) -> f64 {
22+
input + 1.0
23+
}

Lesson_04/src/case_05.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
pub fn run() {
2+
let mut average = 3.14;
3+
println!("Average: {}", average);
4+
decrease_one(&mut average);
5+
println!("After decrease, {}", average);
6+
}
7+
8+
// decrease_one fonksiyonu input verisini referans yoluyla alır
9+
fn decrease_one(input: &mut f64) {
10+
// * operatörü dereference anlamındadır (deref)
11+
// yani & ile işaret ettiğim referans değişkenin asıl değerini almak için * ile ilerleriz
12+
// *input -= 1.0;
13+
let _ = *input -= 1.0;
14+
}

Lesson_04/src/case_06.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
pub fn run() {
2+
let mut message = "Houston".to_string();
3+
println!("{}", message);
4+
change(&mut message);
5+
println!("{}", message);
6+
}
7+
8+
fn change(input: &mut String) {
9+
input.push_str("! We have a problem");
10+
}

Lesson_04/src/case_07.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
pub fn run() {
2+
let message = "Houston";
3+
println!("{}", message);
4+
let new_message = change(&message);
5+
println!("{}", new_message);
6+
}
7+
8+
fn change(input: &str) -> String {
9+
let mut output = String::from(input);
10+
output.push_str("!We have a problem");
11+
output
12+
}

Lesson_04/src/case_08.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
pub fn run() {
2+
let mut super_mario = Player {
3+
id: 1,
4+
title: "Super Mario".to_string(),
5+
location: (0.0, 0.0),
6+
};
7+
println!("{:#?}", super_mario);
8+
jump(&mut super_mario, (1.0, 0.0));
9+
println!("{:#?}", super_mario);
10+
}
11+
12+
#[derive(Debug)]
13+
struct Player {
14+
id: u32,
15+
title: String,
16+
location: (f32, f32),
17+
}
18+
19+
fn jump(player: &mut Player, velocity: (f32, f32)) {
20+
player.location.0 += velocity.0;
21+
player.location.1 += velocity.1;
22+
}

Lesson_04/src/case_09.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
pub fn run() {
2+
// let mut super_mario = Player {
3+
// id: 1,
4+
// title: "Super Mario",
5+
// location: (0.0, 0.0),
6+
// };
7+
let mut super_mario = Player::new(13, "Super Mario", (0.0, 10.0));
8+
println!("{:#?}", super_mario);
9+
jump(&mut super_mario, (1.0, 0.0));
10+
println!("{:#?}", super_mario);
11+
}
12+
13+
#[derive(Debug)]
14+
struct Player<'level> {
15+
id: u32,
16+
title: &'level str,
17+
// title: &'static str, // static lifetime. Yani program çalıştığı müddetçe bu referans yaşayacak anlamında.
18+
location: (f32, f32),
19+
}
20+
21+
impl<'level> Player<'level> {
22+
fn new(id: u32, title: &'level str, location: (f32, f32)) -> Self {
23+
Player {
24+
id,
25+
title,
26+
location,
27+
}
28+
}
29+
}
30+
31+
fn jump(player: &mut Player, velocity: (f32, f32)) {
32+
player.location.0 += velocity.0;
33+
player.location.1 += velocity.1;
34+
}

Lesson_04/src/case_10.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
pub fn run() {
2+
let mut super_mario = Player {
3+
id: 1,
4+
title: "Super Mario".to_string(),
5+
location: (0.0, 0.0),
6+
};
7+
jump(&mut super_mario, (1.0, 0.0));
8+
println!("{:#?}", super_mario);
9+
10+
// let mut brother_mario = super_mario.clone();
11+
let mut brother_mario = super_mario;
12+
brother_mario.title = "Brother Mario".to_string();
13+
jump(&mut brother_mario, (1.0, 0.0));
14+
println!("{:#?}", brother_mario);
15+
// UYARI! Hatayı görmek için aşağıdaki kod satırını açarak ilerleyin
16+
// println!("{:#?}", super_mario); // Burada Value used after being moved [E0382] hatası oluşur.
17+
}
18+
19+
#[derive(Debug, Clone)]
20+
struct Player {
21+
id: u32,
22+
title: String,
23+
location: (f32, f32),
24+
}
25+
26+
fn jump(player: &mut Player, velocity: (f32, f32)) {
27+
player.location.0 += velocity.0;
28+
player.location.1 += velocity.1;
29+
}

Lesson_04/src/main.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,24 @@ mod case_00;
66
mod case_01;
77
mod case_02;
88
mod case_03;
9+
mod case_04;
10+
mod case_05;
11+
mod case_06;
12+
mod case_07;
13+
mod case_08;
14+
mod case_09;
15+
mod case_10;
916

1017
fn main() {
1118
// case_00::run(); // value borrowed here after move hatası
1219
// case_01::run(); // &str kullanımı sebebiyle case 00'daki ihlal oluşmaz
1320
// case_02::run();
14-
case_03::run();
21+
// case_03::run();
22+
// case_04::run();
23+
// case_05::run();
24+
// case_06::run();
25+
// case_07::run();
26+
// case_08::run();
27+
// case_09::run();
28+
case_10::run();
1529
}

0 commit comments

Comments
 (0)