Edited
May 7, 2025 1:56 AM
Tags
programmingrust
Decided to finally lock in and learn Rust once and for all. these are the notes i have on probably the hardest topics i encountered. yea i can vibe code, but its cooler to code when you know what ur writing. I am using zero2prod as well to apply what i learn, and also learn in a functional way.
Questions:
- What is the use / differences behind function signatures that want generics, implemented traits, and generics that implement traits?
- what is code coverage, and how does rust handle it with
cargo-llvm-cov
- what does
.expect()
do? - a method used on
Result<T, E>
orOption<T>
types to unwrap the value—and panic with a custom message if it’s an error orNone
. - good for testing, not good for production level code. its not very graceful
- should use ? instead
Chapter 3:
- What does
#[tokio::test]
do, and how is it different from#[test]
? - Why is binding to
127.0.0.1:0
useful in integration tests? - What’s the difference between
.map()
and.and_then()
on aResult<T, E>
orOption<T>
? - What happens if you call
.await
on a server future returned by Actix's.run()
method? - Explain the difference between
main.rs
andlib.rs
in a Rust binary crate. Why do we move server startup code intolib.rs
?
Clippy
- can mute warnings with:
#[allow(clippy::lint_name)]
attribute
Matching
Async with tokio
- it basically takes in some function body, and then “polls” the code to proceed thru the future
- when expanded, it basically takes in some code block, e.g
body
and then drives the future:
tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.expect("Failed building the Runtime")
.block_on(body)
}
Monads
- Useful when your code may return something, or may return nothing.
- because your function has to have a signature, but maybe it might not have a value to return, for example an error vs a success type return
- other examples include:
- fail (
Result
) - be optional (
Option
) - be deferred (
Iterator
,Future
) - wrap values in context (e.g. logging, side-effects, etc.)