Ownership und Borrowing hinzugefügt

main
Lukas Mueller 2026-05-28 18:53:06 +02:00
parent c82cbd9894
commit 7a08390e01
19 changed files with 172 additions and 2 deletions

View File

View File

@ -0,0 +1,19 @@
#include <iostream>
int main() {
// Allocating memory on the heap
int* myPointer = new int(42);
std::cout << "myPointer at address "
<< myPointer << " is equal to " << *myPointer << std::endl;
// Allocating memory on the heap
myPointer = new int(10);
std::cout << "myPointer at address "
<< myPointer << " is equal to " << *myPointer << std::endl;
delete myPointer;
return 0;
}

View File

@ -0,0 +1,21 @@
fn main() {
let s = String::from("String");
takes_ownership(s);
let x = 5;
makes_copy(x);
println!("{x}");
println!("{s}");
}
fn takes_ownership(some_string: String) {
println!("{some_string}");
}
fn makes_copy(number: i32) {
println!("{number}");
}

View File

@ -0,0 +1,8 @@
#include <iostream>
int main() {
char* ptr1 = new char[5]{'h', 'e', 'l', 'l', 'o'};
char* ptr2 = ptr1;
std::cout << ptr1 << std::endl;
} // Memory leak since nothing is released!

View File

@ -0,0 +1,6 @@
fn main() {
let s1 = String::from("hello");
let s2 = s1;
println!("{s1}, world!");
}

View File

@ -0,0 +1,7 @@
fn main() {
let mut s1 = String::from("hello");
s1.push_str(", world!"); // appending the literal to s
println!("{s1}"); // this will print `hello, world!`
}

View File

@ -0,0 +1,16 @@
fn main() {
let s1 = get_ownership();
let s2 = String::from("hello");
let s3 = take_and_get_ownership(s2);
}
fn get_ownership() -> String {
let s = String::from("yours");
s
}
fn take_and_get_ownership(some_string: String) -> String {
some_string
}

View File

@ -0,0 +1,8 @@
fn main() {
// s is not valid here, since it's not yet declared
{
let s = "hello"; // s is valid from this point forward
// do stuff with s
println!("{s}");
} // s is no longer valid since the scope is over
}

View File

@ -0,0 +1,9 @@
fn main() {
let mut s = String::from("hello");
let r1 = &s;
let r2 = &s;
let r3 = &mut s;
println!("{r1}, {r2}, and {r3}");
}

View File

@ -0,0 +1,9 @@
fn main() {
let reference_to_nothing = dangle();
}
fn dangle() -> String {
let s = String::from("hello");
s
}

View File

@ -0,0 +1,9 @@
#include <iostream>
int main() {
int* ptr1 = new int(42);
int* ptr2 = ptr1;
delete ptr1;
std::cout << "Wert bei " << ptr2 << " ist " << *ptr2 << std::endl;
}

View File

@ -0,0 +1,9 @@
fn main() {
let reference_to_nothing = dangle();
}
fn dangle() -> &String {
let s = String::from("hello");
&s
}

View File

@ -0,0 +1,8 @@
fn main() {
let mut s = String::from("hello");
let r1 = &mut s;
let r2 = &mut s;
println!("{r1}, {r2}");
}

View File

@ -0,0 +1,9 @@
fn main() {
let s = String::from("hello");
change(&s);
}
fn change(some_string: &String) {
some_string.push_str(", world!");
}

View File

@ -0,0 +1,11 @@
fn main() {
let s1 = String::from("hello");
let len = calculate_length(&s1);
println!("The length of '{s1}' is {len}.");
}
fn calculate_length(s: &String) -> usize {
s.len()
}

View File

@ -0,0 +1,9 @@
fn main() {
let mut s = String::from("hello");
{
let r1 = &mut s;
}
let r2 = &mut s;
}

View File

@ -0,0 +1,6 @@
[package]
name = "live-coding"
version = "0.1.0"
edition = "2024"
[dependencies]

View File

@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}

View File

@ -3,7 +3,7 @@ In diesem Repo sind alle Unterlagen für die Präsentation der Programmiersprach
Das Repo beinhaltet: Das Repo beinhaltet:
- Dockerfile (TODO) - Dockerfile (TODO)
- Basics (TODO) - Basics (TODO)
- Ownership und Borrowing (TODO) - Ownership und Borrowing
- Pattern Matching und Enumerationen (TODO) - Pattern Matching und Enumerationen (TODO)
- Testing und Debugging (TODO) - Testing und Debugging (TODO)
@ -16,6 +16,9 @@ Das Repo beinhaltet:
3. Kontrollstrukturen 3. Kontrollstrukturen
4. Types 4. Types
4. Ownership & Borrowing (Live Coding) (ca. 20 Minuten) 4. Ownership & Borrowing (Live Coding) (ca. 20 Minuten)
1. Motivation: Warum Ownership
2. Prinzip: Ownership
3. Prinzip: Borrowing
5. Pattern Matching und Enums (Live Coding) (ca. 20 Minuten) 5. Pattern Matching und Enums (Live Coding) (ca. 20 Minuten)
6. Testing und Debugging (ca. 5 Minuten) 6. Testing und Debugging (ca. 5 Minuten)
7. Anwendungen und interessante Fakten (Nebenläufigkeit, Kernel, usw.) (ca. 10 Minuten) 7. Anwendungen und interessante Fakten (Nebenläufigkeit, Kernel, usw.) (ca. 10 Minuten)
@ -33,4 +36,4 @@ Das Repo beinhaltet:
# Notizen # Notizen
- Cheat Sheet erstellen - Cheat Sheet erstellen
- Tests für die Hausaufgabe erstellen - Tests für die Hausaufgabe erstellen