Rust Part 1
Rust Part 1
Beginner to Advance
Part 1
Part 2
Easy
Medium
Hard
Before we start, lets recap Part 1
Recapping Part 1
Recapping Part 1
1. Steps to install - https://www.rust-lang.org/tools/install
Recapping Part 1
1. Steps to install - https://www.rust-lang.org/tools/install
fi
fi
Recapping Part 1
Q. Write a function b that nds the bbonacci of a number
fi
it takes as input
fi
fi
Mutability
If statement
For loops
Recapping Part 1
Q. Write a function get_string_length that takes a string
as an input and returns its length
Recapping Part 1
Q. Write a function get_string_length that takes a string
as an input and returns its length
De ning a
string
fi
Implicit
Return
Recapping Part 1
Q. Write a function get_string_length that takes a string
as an input and returns its length
Recapping Part 1
Recapping Part 1
Structs let you structure data together
Recapping Part 1
Structs let you structure data together
Recapping Part 1
Implementing structs
Recapping Part 1
Implementing structs
De ning the struct
fi
Implementing
Functions on
the struct
Declaring a variable
Calling the function
Recapping Part 1
Structs let you structure data together
Recapping Part 1
Enums let you enumerate over various types of a value
Recapping Part 1
Enums let you enumerate over various types of a value
Recapping Part 1
Enum as an argument
Recapping Part 1
Enums let you enumerate over various types of a value
Recapping Part 1
Enums with values
Recapping Part 1
Has an associated radius
Recapping Part 1
Has an associated side
Recapping Part 1
Has an associated width and height
Recapping Part 1
Enums let you enumerate over various types of a value
Recapping Part 1
Let you pattern match across various variants
of an enum and run some logic
Recapping Part 1
Let you pattern match across various variants How would you
of an enum and run some logic Implement this?
Recapping Part 1
Recapping Part 1
match on the enum
Recapping Part 1
If type is circle
Recapping Part 1
Then return pi * r * r
Recapping Part 1
If type is square
Recapping Part 1
Then return side * side
Recapping Part 1
If shape is rectangle
Recapping Part 1
Then return width * height
Recapping Part 1
Return the result implicitly
Recapping Part 1
Let you pattern match across various variants How would you
of an enum and run some logic Implement this?
Recapping Part 1
The Option enum lets you return either Some value or None Value
Recapping Part 1
The Option enum lets you return either Some value or None Value
Recapping Part 1
Q: Write a function that returns the index of the rst “a” in a string
fi
Recapping Part 1
Returns an Option of type
I32
Recapping Part 1
If “a” is found
Recapping Part 1
fi
Recapping Part 1
The Result enum lets you return either Ok value or Err Value
The result enum is how you can do error handling in Rust
Recapping Part 1
Q: Write a function that reads the contents of a le
fi
Recapping Part 1
Q: Write a function that reads the contents of a le
fi
Returns a Result
Pattern matching
on the result
Recapping Part 1
You can add an external crate to your project by running
cargo add crate_name
Recapping Part 1
You can add an external crate to your project by running
cargo add chrono
Use from external crate
(Similar to import)
Call a fn from an external crate
Recapping Part 1
Memory (16gb)
CPU GPU
Memory management
Mac Machine
Memory (16gb)
Memory management
Mac Machine
Memory (16gb)
32 bits
Memory management
Memory (16gb)
32 bits
32 bits
Memory management
Memory (16gb)
32 bits
32 bits
32 bits
Memory management
Memory (16gb)
Stack Heap
Memory management
Memory (16gb)
Stack Heap
Stack frame
Memory management
Stack Heap
Stack Heap
Stack Heap
Stack Heap
Stack Heap
Stack Heap
Stack Heap
Memory (16gb)
Stack Heap
???
Memory management
Stack Heap
Stack Heap
Memory management
Memory (16gb)
Stack Heap
h
e
l
l
32 bits
O
Memory management
Memory (16gb)
Stack Heap
h
e
Pointer to
l
Why are strings stored on the heap?
l
1. They are large 32 bits
2. Their size can change at runtime, and the O
size of a stack frame needs to be xed
fi
Recapping Part 1
Stack Heap
Recapping Part 1
Recapping Part 1
In rust, all variables are immutable by default
You have to explicitly set them as mut
Recapping Part 1
In rust, all variables are immutable by default
You have to explicitly set them as mut
Immutable by default
Recapping Part 1
In rust, all variables are immutable by default
You have to explicitly set them as mut
Recapping Part 1
Recapping Part 1
We need to understand 4 jargon
1. Ownership
2. Moving
3. Borrowing
4. References
Three ways of doing Memory management
This C++ code has a memory leak
This C++ code has a memory leak
This C++ code has a memory leak
This C++ code has a memory leak
Stack Heap
This C++ code has a memory leak
Stack Heap
This C++ code has a memory leak
Stack Heap
This C++ code has a memory leak
Stack Heap
Three ways of doing Memory management
Three ways of doing Memory management
Is there a memory leak?
Is there a memory leak?
Stack Heap
Is there a memory leak?
Stack Heap
Is there a memory leak?
Stack Heap
h
e
l
32 bits
l
O
Is there a memory leak?
Stack Heap
h
e
l
32 bits
l
O
Is there a memory leak?
Does the heap get cleaned up?
Stack Heap
h
e
l
O
Is there a memory leak?
Does the heap get cleaned up?
Yes
Stack Heap
Ownership rules
Ownership
1. Ownership
2. Moving
3. Borrowing
4. References
Move
Move
1 2
❌ ❌
Move
✅
Move
Move
Move
1. Ownership
2. Moving
3. Borrowing
4. References
Borrowing
We pass in a reference to s1
s1 is borrowed by print_str
s1 is still owned by create_string
Borrowing
De ne variables s1
fi
Borrowing
Take mutable
reference #1
Borrowing
Take mutable
reference #2
Borrowing
Use mutable
reference #1
Borrowing
We need to understand 4 jargon
1. Ownership
2. Moving
3. Borrowing
4. References
Recapping Part 1
Recapping Part 1
Lets get into Part #2