Ownership und Borrowing hinzugefügt
parent
c82cbd9894
commit
7a08390e01
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -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}");
|
||||
}
|
||||
|
|
@ -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!
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
fn main() {
|
||||
let s1 = String::from("hello");
|
||||
let s2 = s1;
|
||||
|
||||
println!("{s1}, world!");
|
||||
}
|
||||
|
|
@ -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!`
|
||||
}
|
||||
|
|
@ -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
|
||||
}
|
||||
|
|
@ -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
|
||||
}
|
||||
|
|
@ -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}");
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
fn main() {
|
||||
let reference_to_nothing = dangle();
|
||||
}
|
||||
|
||||
fn dangle() -> String {
|
||||
let s = String::from("hello");
|
||||
|
||||
s
|
||||
}
|
||||
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
fn main() {
|
||||
let reference_to_nothing = dangle();
|
||||
}
|
||||
|
||||
fn dangle() -> &String {
|
||||
let s = String::from("hello");
|
||||
|
||||
&s
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
fn main() {
|
||||
let mut s = String::from("hello");
|
||||
|
||||
let r1 = &mut s;
|
||||
let r2 = &mut s;
|
||||
|
||||
println!("{r1}, {r2}");
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
fn main() {
|
||||
let s = String::from("hello");
|
||||
|
||||
change(&s);
|
||||
}
|
||||
|
||||
fn change(some_string: &String) {
|
||||
some_string.push_str(", world!");
|
||||
}
|
||||
|
|
@ -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()
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
fn main() {
|
||||
let mut s = String::from("hello");
|
||||
|
||||
{
|
||||
let r1 = &mut s;
|
||||
}
|
||||
|
||||
let r2 = &mut s;
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
[package]
|
||||
name = "live-coding"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
|
|
@ -3,7 +3,7 @@ In diesem Repo sind alle Unterlagen für die Präsentation der Programmiersprach
|
|||
Das Repo beinhaltet:
|
||||
- Dockerfile (TODO)
|
||||
- Basics (TODO)
|
||||
- Ownership und Borrowing (TODO)
|
||||
- Ownership und Borrowing
|
||||
- Pattern Matching und Enumerationen (TODO)
|
||||
- Testing und Debugging (TODO)
|
||||
|
||||
|
|
@ -16,6 +16,9 @@ Das Repo beinhaltet:
|
|||
3. Kontrollstrukturen
|
||||
4. Types
|
||||
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)
|
||||
6. Testing und Debugging (ca. 5 Minuten)
|
||||
7. Anwendungen und interessante Fakten (Nebenläufigkeit, Kernel, usw.) (ca. 10 Minuten)
|
||||
|
|
@ -33,4 +36,4 @@ Das Repo beinhaltet:
|
|||
|
||||
# Notizen
|
||||
- Cheat Sheet erstellen
|
||||
- Tests für die Hausaufgabe erstellen
|
||||
- Tests für die Hausaufgabe erstellen
|
||||
|
|
|
|||
Loading…
Reference in New Issue