ownership completed
This commit is contained in:
@@ -1,3 +1,9 @@
|
|||||||
|
mod ownership;
|
||||||
|
mod references_and_borrowing;
|
||||||
|
mod slice_type;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("Hello, world!");
|
ownership::run();
|
||||||
|
references_and_borrowing::run();
|
||||||
|
slice_type::run();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,30 @@
|
|||||||
|
pub fn run() {
|
||||||
|
println!("\n=== Ownership ===\n");
|
||||||
|
|
||||||
|
// The stack and the heap
|
||||||
|
// So ownership is a set of rules that govern how Rust managages memory
|
||||||
|
// arrays and simple variables are allocated to the stack. These have fixed and known lentghs at
|
||||||
|
// compile time. This is fast on the stack.
|
||||||
|
// However, when working with data of unknown lengths, for example String::new(), Rust cannot
|
||||||
|
// know the length at compile time. This is then allocated to the heap with a reference in memory.
|
||||||
|
|
||||||
|
// OWNERSHIP RULES
|
||||||
|
// Each value in Rust has an owner
|
||||||
|
// There can only be one owner at a time
|
||||||
|
// When the owner goes out of scope, the value is dropped.
|
||||||
|
|
||||||
|
// VARIABLE SCOPE
|
||||||
|
|
||||||
|
// This is a string literal. This is hardcoded into the program at compile time.
|
||||||
|
|
||||||
|
{ // s is not valid here, since it has not been declared
|
||||||
|
let s = "hello"; // s is valid from this point onward
|
||||||
|
println!("{s}");
|
||||||
|
// Do stuff with s
|
||||||
|
|
||||||
|
} // this scope is now over, and the variable s is no longer valid
|
||||||
|
// when s comes into scope, it is valid
|
||||||
|
// when s goes out of scope, it is invalid
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
pub fn run() {
|
||||||
|
println!("\n=== references and borrowing ===\n");
|
||||||
|
// We can provide a reference to a String value.
|
||||||
|
// A reference is like a pointer to an address in memory, which we can follow to access the data.
|
||||||
|
// The data itself is owned by some other variable.
|
||||||
|
// This is however not a pointer, because a reference guarantees to point at a valid value of a
|
||||||
|
// particular type for the life of the reference.
|
||||||
|
//
|
||||||
|
// Here is how you can use a variable as a reference to a parameter without taking ownership of
|
||||||
|
// it
|
||||||
|
|
||||||
|
|
||||||
|
// s is a reference to s1 which has a reference to an address in memory
|
||||||
|
let s1 = String::from("hello");
|
||||||
|
let len = calculate_length(&s1);
|
||||||
|
println!("The length of '{s1}' is {len}");
|
||||||
|
|
||||||
|
let mut str2 = String::from("hello");
|
||||||
|
change(&mut str2);
|
||||||
|
|
||||||
|
// At any given time, you can have either one mutable reference or any number of immutable references.
|
||||||
|
// References must always be valid.
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
fn calculate_length(s: &String) -> usize { // s is a reference to a string
|
||||||
|
s.len()
|
||||||
|
} // Here, s goes out of scope. But because s does not have ownership of what it refers to
|
||||||
|
// The String is not dropped.
|
||||||
|
|
||||||
|
// THE ACTION OF CREATING A REFERENCE IS **BORROWING**
|
||||||
|
// As it is in real life. You borrow something, and then you give it back. You do not own it.
|
||||||
|
|
||||||
|
// Here is an example that will not work
|
||||||
|
// fn main() {
|
||||||
|
// let s = String::from("Hello");
|
||||||
|
//
|
||||||
|
// change (&s);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// fn change(some_string: &String) {
|
||||||
|
// some_string.push_str(", world")
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// A reference is by default also immutable. We are not allowed to modify something we have a
|
||||||
|
// reference to.
|
||||||
|
//
|
||||||
|
// MUTABLE REFERENCES
|
||||||
|
|
||||||
|
fn change(some_string: &mut String) {
|
||||||
|
some_string.push_str(", world!");
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
pub fn run() {
|
||||||
|
println!("\n === slice types ===\n");
|
||||||
|
// A slice is a typa of reference, so it does not have ownership
|
||||||
|
// A string slice is a reference to a contiguous sequence of the elements of a string, and looks
|
||||||
|
// like this:
|
||||||
|
let s = String::from("hello world");
|
||||||
|
|
||||||
|
let _hello = &s[0..5];
|
||||||
|
let _world = &s[6..11];
|
||||||
|
|
||||||
|
// now hello is a reference to part of the string, and let world is a reference to another part
|
||||||
|
// of the string.
|
||||||
|
//
|
||||||
|
let f_word = first_word(&s);
|
||||||
|
println!("{f_word}");
|
||||||
|
|
||||||
|
|
||||||
|
let my_string = String::from("hello world");
|
||||||
|
|
||||||
|
// `first_word` works on slices of `String`s, whether partial or whole.
|
||||||
|
let _word = first_word(&my_string[0..6]);
|
||||||
|
let _word = first_word(&my_string[..]);
|
||||||
|
// `first_word` also works on references to `String`s, which are equivalent
|
||||||
|
// to whole slices of `String`s.
|
||||||
|
let _word = first_word(&my_string);
|
||||||
|
|
||||||
|
let my_string_literal = "hello world";
|
||||||
|
|
||||||
|
// `first_word` works on slices of string literals, whether partial or
|
||||||
|
// whole.
|
||||||
|
let _word = first_word(&my_string_literal[0..6]);
|
||||||
|
let _word = first_word(&my_string_literal[..]);
|
||||||
|
|
||||||
|
// Because string literals *are* string slices already,
|
||||||
|
// this works too, without the slice syntax!
|
||||||
|
let _word = first_word(my_string_literal);
|
||||||
|
|
||||||
|
let _a = [1, 2, 3, 4, 5];
|
||||||
|
// Just as we might want to refer to part of a string, we might want to refer to part of an array. We’d do so like this:
|
||||||
|
|
||||||
|
let a = [1, 2, 3, 4, 5];
|
||||||
|
|
||||||
|
let slice = &a[1..3];
|
||||||
|
|
||||||
|
assert_eq!(slice, &[2, 3]);
|
||||||
|
// This slice has the type &[i32].
|
||||||
|
//
|
||||||
|
// The concepts of ownership, borrowing, and slices ensure memory safety in Rust programs at compile time. The Rust language gives you control over your memory usage in the same way as other systems programming languages. But having the owner of data automatically clean up that data when the owner goes out of scope means you don’t have to write and debug extra code to get this control.
|
||||||
|
|
||||||
|
// Ownership affects how lots of other parts of Rust work.
|
||||||
|
}
|
||||||
|
|
||||||
|
fn first_word(s: &str) -> &str {
|
||||||
|
let bytes = s.as_bytes();
|
||||||
|
|
||||||
|
for (i, &item) in bytes.iter().enumerate() {
|
||||||
|
if item == b' ' {
|
||||||
|
return &s[0..i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
&s[..]
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user