Skip to content

Cargo, packages and external deps

Course: Rust Bootcamp - chapter 22

Just like the nodejs ecosystem has npm, the rust ecosystem has cargo

Cargo is a package manager for rust, which means we can use it to bring packages (crates in case of rust) to our projects

Use crate - https://crates.io/crates/rand

Run cargo add rand

use rand::{Rng, thread_rng};
fn main() {
let mut rng = thread_rng();
let n: u32 = rng.gen();
println!("Random number: {}", n);
}

Use crate https://crates.io/crates/chrono

Run cargo add chrono

use chrono::{Local, Utc};
fn main() {
// Get the current date and time in UTC
let now = Utc::now();
println!("Current date and time in UTC: {}", now);
// Format the date and time
let formatted = now.format("%Y-%m-%d %H:%M:%S");
println!("Formatted date and time: {}", formatted);
// Get local time
let local = Local::now();
println!("Current date and time in local: {}", local);
}

What all libraries does rust have?

A lot of them

  1. https://actix.rs/ - Extremely fast http server
  2. https://serde.rs/ - Serializing and deserialising data in rust
  3. https://tokio.rs/ - Asynchronous runtime in rust
  4. https://docs.rs/reqwest/latest/reqwest/ - Send HTTP requests
  5. https://docs.rs/sqlx/latest/sqlx/ - Connect to sql database