Rust Part 2
Rust Part 2
Collections
Rust’s standard library includes a number of very useful data structures called collections.
Most other data types represent one speci c value, but collections can contain multiple values.
the data these collections point to is stored on the heap
fi
Vectors
Vectors
Approach #1
Vectors
Approach #2
Vectors
Methods -
1. insert
2. get
3. remove
4. clear
Hashmap
Q: Write a function that takes a vector of tuples (each tuple containing a key and a
value) and returns a Hashmap where the keys are the unique keys from the input tuples
and the values are vectors of all corresponding values associated with each key.
Hashmap
Q: Write a function that takes a vector of tuples (each tuple containing a key and a
value) and returns a Hashmap where the keys are the unique keys from the input tuples
and the values are vectors of all corresponding values associated with each key.
Iterators
Iterators
1. Iterating using for loops
Iterators
2. Iterating after creating an `iterator`
Iterators
2. Iterating after creating an `iterator`
Useful when
1. You no longer need the
original collection
2. When you need to squeeze
performance bene ts by
transferring ownership
(avoiding references)
fi
Iterators
5. IntoIter
The for syntax when applied directly on the collection uses into_iter under the hood
same
Iterators
Which to chose?
Iter IterMut IterInto
Iterator adaptors are methods de ned on the Iterator trait that don’t consume the
iterator. Instead, they produce di erent iterators by changing some aspect of the
original iterator.
ff
fi
Iterators
Iterator adaptors are methods de ned on the Iterator trait that don’t consume the
iterator. Instead, they produce di erent iterators by changing some aspect of the
original iterator.
1. map ff
fi
Iterators
Iterator adaptors are methods de ned on the Iterator trait that don’t consume the
iterator. Instead, they produce di erent iterators by changing some aspect of the
original iterator.
1. map
2. lter
fi
ff
fi
Iterators
Assignment -
Write the logic to rst lter all odd values then double each value and create a new
vector
fi
fi
Iterators
Assignment -
Write the logic to rst lter all odd values then double each value and create a new
vector
fi
fi
Iterators
Assignment -
Write the logic to rst lter all odd values then double each value and create a new
vector
fi
fi
Iterators in Hashmaps
Strings vs slices
Strings
1. Creating a string
Strings
1. Creating a string
2. Mutating a string
Strings
1. Creating a string
2. Mutating a string
3. Deleting from a string
Strings
Slices
Slices
Problem -
1. We take up double the memory
2. If the `name` string gets `cleared` ,
`ans` still has `hello` as the value in it
fi
Slices
Q: Write a function that takes a string as an input
And returns the rst word from it
Problem -
1. We take up double the memory
2. If the `name` string gets `cleared` ,
`ans` still has `hello` as the value in it
Name
Approach #1 - Return a new string
Problem -
1. We take up double the memory
2. If the `name` string gets `cleared` ,
`ans` still has `hello` as the value in it
Ans
Ans
fi
Slices
Q: Write a function that takes a string as an input
And returns the rst word from it
Approach #2 (with slices)
Ans
fi
Slices
Q: Write a function that takes a string as an input
And returns the rst word from it
Approach #2 (with slices) (alt)
Name
Ans
fi
Slices
3 types of commonly used strings (there are actually much more)
Slices
Slices can also be applied to other collections like vectors/arrays
Generics
From the live meet-up
Impl blocks
From the live meet-up
Traits
Traits
Traits
De ning the trait
fi
Traits
De ning a struct
fi
Traits
Implementing a
Trait on the struct
Traits
De ning the
`default implementation`
fi
Traits
Default implementations
Trait as an argument
Traits
Traits as parameters
longest_str is de ned
fi
Lifetimes
Q - Write a function that takes two
strings as an input
And returns the bigger amongst them
str1 is de ned
fi
Lifetimes
Q - Write a function that takes two
strings as an input
And returns the bigger amongst them
Str2 is de ned
fi
Lifetimes
Q - Write a function that takes two
strings as an input
And returns the bigger amongst them
longest_str is valid
Lifetimes
Points to nothing
Lifetimes
Q - Write a function that takes two string references as an input
And returns the bigger amongst them
Condition checks
Lifetimes
Q - Write a function that takes two string references as an input
And returns the bigger amongst them
Describe a relationship
b/w the lifetimes of input args
and output args
fi
Lifetimes
How to x the error? - Specify lifetimes
Describe a relationship
b/w the lifetimes of input args
and output args
Describe a relationship
b/w the lifetimes of input args
and output args
Or more technically, the shorter lifetimes is what the return type will have
fi
Lifetimes
Lifetimes
Now if you run this code,
it fails with a better error
Lifetimes
Now if you run this code, Live long enough = lifetime of return value ends
it fails with a better error
Structs with lifetimes
Until now, we haven’t used references inside a struct
Lets try that
Structs with lifetimes
Until now, we haven’t used references inside a struct
Lets try that
Structs with lifetimes
Until now, we haven’t used references inside a struct
Lets try that
Structs with lifetimes
Until now, we haven’t used references inside a struct
Lets try that
Structs with lifetimes
Why do you need structs with references to have a lifetime parameter?
Structs with lifetimes
Why do you need structs with references to have a lifetime parameter?
So we know how long the `struct` can live
Structs with lifetimes
Structs with lifetimes
✅
Multithreading
Multithreading
Thread 1 Thread 2
Core 1 Core 2 Core 3 Core 4 Core 5
Thread 1 Thread 2
Core 1 Core 2 Core 3 Core 4 Core 5
Use case?
One thread reading data from redis ,
other thread processing it
Message passing
Pass a variable over
Thread 1 Thread 2
Core 1 Core 2 Core 3 Core 4 Core 5
Use case?
One thread reading data from redis ,
other thread processing it
Message passing
Channels
Thread 1 Thread 2
Core 1 Core 2 Core 3 Core 4 Core 5
Can you write the code that nds the sum from 1 - 10^8?
fi
Message passing
Channels
Can you write the code that nds the sum from 1 - 10^8?
Use threads to make sure you use all cores of your machine
fi
Message passing
Channels
Can you write the code that nds the sum from 1 - 10^8?
Use threads to make sure you use all cores of your machine
Can you write the code that nds the sum from 1 - 10^8?
fi
Message passing
This code almost works
Message passing
This code almost works
Can you guess what goes wrong here?
Message passing
This code almost works
Can you guess what goes wrong here?
Right solution
Async rust