0% found this document useful (0 votes)
22 views

Rust Part 1

Rust notes part 1

Uploaded by

Boot Neck
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Rust Part 1

Rust notes part 1

Uploaded by

Boot Neck
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 159

Rust bootcamp

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

2. Install VSCode Extensions - rust-analyser , CodeLLDB, toml


Recapping Part 1
Recapping Part 1
1. Start a project (binary)
Recapping Part 1
1. Start a project (binary)

2. Start a project (library)


Recapping Part 1
Recapping Part 1
Q. Write a function is_even that takes a number as an input
and returns true if it is even
Recapping Part 1
Q. Write a function is_even that takes a number as an input
and returns true if it is even
De ning the
main function
fi
Calling a function
De ning a
function
fi
Name of the function
Arguments
Return type
Function Body
Recapping Part 1
Q. Write a function is_even that takes a number as an input
and returns true if it is even
Recapping Part 1
Q. Write a function b that nds the bbonacci of a number
fi
it takes as input

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

De ning the enum


fi
Recapping Part 1

Declaring the enum


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

Returns the Some variant


of the enum
Recapping Part 1

Else it returns the None variant


Recapping Part 1

We pattern match on the result


Recapping Part 1
Q: Write a function that returns the index of the rst “a” in a string

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

That covers the easy things from Part 1


Recapping Part 1

More OS speci c concepts


fi
Recapping Part 1
Memory management
What is memory?
Mac Machine

Memory (16gb)

CPU GPU
Memory management
Mac Machine

Memory (16gb)
Memory management
Mac Machine

Memory (16gb)

How much space do you think


A program needs?
Memory management
Memory (16gb)
Memory management
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

32 bits 32 bits 32 bits

Stack frame
Memory management

What do you think happens here?


Memory management
Memory (16gb)

Stack Heap

32 bits 32 bits 32 bits


Memory management
Memory (16gb)

Stack Heap

32 bits 32 bits 32 bits


Memory management
Memory (16gb)

Stack Heap

32 bits 32 bits 32 bits


Memory management
Memory (16gb)

Stack Heap

32 bits 32 bits 32 bits

32 bits 32 bits 32 bits


Memory management
Memory (16gb)

Stack Heap

32 bits 32 bits 32 bits

32 bits 32 bits 32 bits


Memory management
Memory (16gb)

Stack Heap

32 bits 32 bits 32 bits

32 bits 32 bits 32 bits


Memory management
Memory (16gb)

Stack Heap

32 bits 32 bits 32 bits


Memory management
Until now, we’ve only stored things on the stack
Lets try to store things on the heap next

Memory (16gb)

Stack Heap

???
Memory management
Stack Heap

Dynamic, allocated at Static, allocated at


runtime compile time

Much larger in size Smaller in size

Slower due to dynamic allocation


and deallocation Faster

Used for Small, xed-size


Used for Dynamic and large data
structures (e.g., Vec, HashMap, Box)
variables and function call
information
fi
Memory management
Stored on the stack Stored on the heap

1. Numbers - i32, u64, f32 1. Strings


2. Booleans - true, false 2. Vectors
3. Fixed sized arrays - [i32; 4] 3. HashMap
4. Structs - { x: i32, y: i32 } 4. Large Arrays/Structs that can’t
5. References (later) t in the Stack
fi
Memory management
Memory (16gb)

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

Allocates data on the heap

Stack Heap
This C++ code has a memory leak

Copies over data to the heap

Stack Heap
This C++ code has a memory leak

Prints the data on screen

Stack Heap
This C++ code has a memory leak

Data is still alive on the heap


Even though there is no reference to the string
(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

S is the owner of the value “hello”


Ownership

S goes out of scope,


value gets removed from the heap
We need to understand 4 jargon

1. Ownership
2. Moving
3. Borrowing
4. References
Move
Move

1 2

❌ ❌
Move


Move
Move
Move

You can return the value from a function


Back to the function calling it
We need to understand 4 jargon

1. Ownership
2. Moving
3. Borrowing
4. References
Borrowing

This code is ugly


(you have to return the value back)
Borrowing

We pass in a reference to s1
s1 is borrowed by print_str
s1 is still owned by create_string
Borrowing

No Borrowing - Error ❌ Borrowing - No error ✅


Borrowing
What if you want the borrow to mutate the value?
Borrowing
What if you want the borrow to mutate the value?
Borrowing
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

You might also like