Ownership
fn main() {
let nums = vec![2, 4, 6];
let nums2 = nums;
println!("{:?}", nums2);
}
Immutable Borrow
fn main() {
let nums = vec![2, 4, 6];
print_nums(nums);
println!("{:?}", nums);
}
fn print_nums(nums: Vec<i32>) {
for num in nums.iter() {
println!("{}", num)
}
}
Multiple immutable borrow
fn main() {
let nums = vec![2, 4, 6];
let nums2 = &nums;
let nums3 = &nums;
let nums4 = &nums;
println!("{:?} {:?} {:?}", nums2, nums3, nums4);
}
Mutable borrow
fn main() {
let nums = vec![2, 4, 6];
extend_nums(nums);
println!("{:?}", nums);
}
fn extend_nums(mut nums: Vec<i32>) {
nums.push(8);
}
- Classic struct -
Color
- Create object and access values
- Debug print
- Mutability of values
- Update for Tuple Struct
- Unit Struct
- Struct of a package with strings and weight
- Impl of ownership
- Impl of reference
- How to call a reference?
- Impl of add weight
- How to call a mut reference?
- Example of
Message
-Echo
,Move
,ChangeColor
,Quit
- If else
enum
and PartialEq
impl PartialEq for Message {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Message::Quit, Message::Quit) => true,
(Message::Echo, Message::Echo) => true,
(Message::Move, Message::Move) => true,
(Message::ChangeColor, Message::ChangeColor) => true,
_ => false,
}
}
}
- Enum with values
- Print - Debug
- Impl for call
- Match
String::from
forfoobar
- How to get a slice from a string
- Trim string
- Replace
bar
withbaz
- Replacement needs a new String. Why?
- String concatenation
push_str
format!
- Crate root
- mod
foo
and how to call - mod
bar
and how to callfoo
from there - export and import
- Separate into files
- Separate into folders