mirror of
https://github.com/darkicewolf50/RustBrock.git
synced 2025-07-06 11:07:12 -06:00
finished ch4
This commit is contained in:
@ -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!`
|
||||
```
|
Reference in New Issue
Block a user