<Re>learning to code {Rust}
👨🏻‍💻

<Re>learning to code {Rust}

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> or Option<T> types to unwrap the value—and panic with a custom message if it’s an error or None.
    • good for testing, not good for production level code. its not very graceful
      • should use ? instead

Chapter 3:

  1. What does #[tokio::test] do, and how is it different from #[test]?
  2. Why is binding to 127.0.0.1:0 useful in integration tests?
  3. What’s the difference between .map() and .and_then() on a Result<T, E> or Option<T>?
  4. What happens if you call .await on a server future returned by Actix's .run() method?
  5. Explain the difference between main.rs and lib.rs in a Rust binary crate. Why do we move server startup code into lib.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.)

Generics

Traits

Enums

Lifetimes

Macros