Skip to content

External packages (crates)

ref - https://doc.rust-lang.org/book/ch07-00-managing-growing-projects-with-packages-crates-and-modules.html

cargo add module_name

Cargo.toml

Screenshot 2025-02-21 at 3.52.25 PM.png

[package]
name = "rust"
version = "0.1.0"
edition = "2021"
[dependencies]
reqwest = "0.12.12"

Features in external crates

You can optionally get some features of an external crate if you dont need the full crate/need some extra features when installing the crate.

[dependencies]
reqwest = { version = "0.12.12", features = ["json"], }
serde = { version = "0.12.12", features = ["derive"] }

Try some packages locally

  1. chrono
  2. dotenv
  3. uuid
  4. tui
  5. thiserror
  6. sqlx

chrono

Chrono lets you do data and time in rust

https://docs.rs/chrono/latest/chrono

Screenshot 2025-02-21 at 4.19.20 PM.png

use chrono::{Local, Utc};
fn main() {
let utc_time = Utc::now();
let local_time = Local::now();
println!("local time is {}", utc_time);
println!("native time is {}", local_time);
}

dotenv

use dotenv::dotenv;
use std::env;
fn main() {
dotenv().ok();
let var = env::var("REDIS_ADDRESS").unwrap();
println!("{}", var);
}

uuid

use uuid::Uuid;
fn main() {
let random_uuid = Uuid::new_v4();
println!("{}", random_uuid);
}

Cargo.toml

uuid = {version = "1.14.0", features = ["v4", "fast-rng"]}

If you dont add the v4 feature, notice you cant access the new_v4 function

Screenshot 2025-02-21 at 4.37.19 PM.png