finished ch4

This commit is contained in:
2025-01-02 14:12:46 -07:00
parent e6e5ab1a0f
commit 2e3f133c61
8 changed files with 395 additions and 4 deletions

View File

@ -281,4 +281,32 @@ for example inputting 7 into the program
this would cause a runtime error
the program would output an error because it didnt get to the final line println! before exiting
it casue the program to exit before attempting to access the invalid space
this is a form of safe memory management that rust name
this is a form of safe memory management that rust name
# Complex Data Type
## String Literal
This is a string literal it is hardcoded into a program
Always immutable
Fast and efficient, stored on the stack, property of being immuatable not of any real value
```rust
let s: &str = "hello";
```
## String
This is a string that is stored on the heap, this can store data unkown (size, char, etc) to you at compile time
Can be mutable, but must request space on the heap then return that memory to the heap, will be returned as soon as it is no longer valid (it calls the drop method from String)
not as fast and efficient
Example of a string being created form a string literal
```rust
let ab:String = String::from("hello");
```
String concatinization example
```rust
let mut s = String::from("hello");
s.push_str(", world!"); // push_str() appends a literal to a String
println!("{s}"); // This will print `hello, world!`
```