Building a Rust Crate Recommender:



This content originally appeared on DEV Community and was authored by Frank Lawrence

Introduction

Finding the right libraries (crates) in Rust can be time-consuming. To solve this, I built a Rust Crate Recommender, a command-line tool that helps developers discover and save Rust libraries based on keywords. This project aligns with the Codecademy Recommendation Software Project, requiring skills in data structures, algorithms, Git version control, and command-line navigation.

In this blog, I’ll walk you through the design, implementation, and skills used to build this project, as well as how it can be extended further.

Project Overview

What Does This Program Do?

  • Users enter a keyword (e.g., async, web, database).
  • The program fetches matching Rust crates from crates.io (Rust’s package registry).
  • It displays results, including:
    • Crate name 📌
    • Description 📖
    • Download count 📥
    • Documentation link 🔗
  • Users can select dependencies, which are saved to a file (dependencies.txt).
  • The program continues running until the user decides to exit.

This tool helps Rust developers quickly find and store useful crates instead of manually searching crates.io.

Key Features & Implementation

1⃣ Storing Data in a Data Structure

  • We use a Vec<CrateInfo>** (vector of structs)** to store fetched crate data.
  • Each crate is represented as a struct:
  #[derive(Debug, Deserialize)]
  pub struct CrateInfo {
      pub id: String,
      pub description: Option<String>,
      pub downloads: u64,
      pub documentation: Option<String>,
  }
  • This allows for fast iteration, sorting, and filtering of the results.

2⃣ Using an Algorithm to Sort and Search Data

  • The results are sorted by downloads (highest first) using Rust’s built-in sort_by():
  crates.sort_by(|a, b| b.downloads.cmp(&a.downloads));
  • Users can filter results based on available documentation.

3⃣ Implementing User Interaction (Command-Line Navigation)

  • The program runs continuously, prompting the user for input until they exit.
  • Users select crates using numbered options, and their selections are saved.

Example interaction:

Enter a keyword to search for crates (or 'exit' to quit): web

Recommended Rust Crates:
1. tokio - An async runtime for Rust (287M downloads)
   📖 Docs: https://docs.rs/tokio
2. warp - A web framework for async Rust (98M downloads)
   📖 Docs: https://docs.rs/warp

Enter the number of a crate to add to your dependencies: 1
Added 'tokio' to your dependencies.

4⃣ Saving Data for Future Use

  • The program stores selected dependencies in dependencies.txt:
  fn save_dependencies(dependencies: &[(String, String)]) {
      let mut file = OpenOptions::new()
          .create(true)
          .append(true)
          .open("dependencies.txt")
          .expect("Failed to open file");

      for (crate_name, doc_link) in dependencies {
          writeln!(file, "{} - Documentation: {}", crate_name, doc_link)
              .expect("Failed to write to file");
      }
  }
  • This allows developers to keep track of their selected crates.

Skills Demonstrated in This Project

✅ 1. Data Structures & Algorithms

  • Vector (Vec<CrateInfo>) for storing crate data.
  • Sorting algorithm (sort_by()) to rank results by popularity.
  • Filtering (retain()) to remove crates with missing descriptions.

✅ 2. API Requests & JSON Handling

  • Uses reqwest to fetch data from crates.io:
  let response = reqwest::get(&url).await?.text().await?;
  let crate_response: CrateResponse = serde_json::from_str(&response)?;
  • Parses JSON using serde to extract relevant information.

✅ 3. Git Version Control

  • The project was managed using Git with regular commits.
  • The repository is available on GitHub.

✅ 4. Command-Line & File Navigation

  • Uses standard input (io::stdin) for user interaction.
  • Writes to a file (dependencies.txt) to store user selections.

Possible Future Improvements

Although the current implementation works well, here are some potential enhancements:

  • 🔍 Fuzzy Search → Improve keyword matching for better recommendations.
  • 📂 Auto-Add to Cargo.toml → Instead of a text file, directly modify Cargo.toml.
  • 📊 Pagination for Large Results → If many crates match, allow users to scroll through pages.
  • ⏩ Caching API Responses → Avoid repeated API calls for the same search.

Building this Rust Crate Recommender was a great way to apply Rust programming concepts, including:

  • Working with APIs (reqwest)
  • Handling JSON (serde)
  • Sorting & filtering data
  • Writing interactive CLI applications
  • Using Git for version control


This content originally appeared on DEV Community and was authored by Frank Lawrence