Convert Figma logo to code with AI

TheAlgorithms logoRust

All Algorithms implemented in Rust

22,076
2,151
22,076
8

Top Related Projects

96,644

Empowering everyone to build reliable and efficient software.

48,187

ripgrep recursively searches directories for a regex pattern while respecting your gitignore

33,285

A simple, fast and user-friendly alternative to 'find'

48,640

A cat(1) clone with wings.

56,041

A cross-platform, OpenGL terminal emulator.

44,714

☄🌌️ The minimal, blazing-fast, and infinitely customizable prompt for any shell!

Quick Overview

TheAlgorithms/Rust is a GitHub repository that provides implementations of various algorithms and data structures in the Rust programming language. It serves as an educational resource for learning about algorithms and Rust, offering a wide range of implementations from basic to advanced concepts.

Pros

  • Comprehensive collection of algorithms and data structures in Rust
  • Well-organized codebase with clear directory structure
  • Educational resource for learning both algorithms and Rust programming
  • Active community with regular contributions and updates

Cons

  • Some implementations may not be optimized for production use
  • Documentation for individual algorithms could be more extensive
  • May lack some advanced or specialized algorithms
  • Not intended as a plug-and-play library for direct use in projects

Code Examples

Here are a few examples of algorithms implemented in the repository:

  1. Binary Search
pub fn binary_search<T: Ord>(item: &T, arr: &[T]) -> Option<usize> {
    let mut left = 0;
    let mut right = arr.len();

    while left < right {
        let mid = left + (right - left) / 2;
        if &arr[mid] == item {
            return Some(mid);
        } else if &arr[mid] > item {
            right = mid;
        } else {
            left = mid + 1;
        }
    }

    None
}
  1. Bubble Sort
pub fn bubble_sort<T: Ord>(arr: &mut [T]) {
    let mut sorted = false;
    let mut n = arr.len();
    while !sorted {
        sorted = true;
        for i in 0..n - 1 {
            if arr[i] > arr[i + 1] {
                arr.swap(i, i + 1);
                sorted = false;
            }
        }
        n -= 1;
    }
}
  1. Fibonacci Sequence
pub fn fibonacci(n: u32) -> u32 {
    match n {
        0 => 0,
        1 | 2 => 1,
        _ => fibonacci(n - 1) + fibonacci(n - 2),
    }
}

Getting Started

To use the algorithms in your Rust project:

  1. Clone the repository:

    git clone https://github.com/TheAlgorithms/Rust.git
    
  2. Navigate to the desired algorithm file.

  3. Copy the implementation into your project or use it as a reference for your own implementation.

  4. Import and use the function in your Rust code:

    mod algorithm;
    use algorithm::binary_search;
    
    fn main() {
        let arr = vec![1, 3, 5, 7, 9];
        let result = binary_search(&5, &arr);
        println!("Index of 5: {:?}", result);
    }
    

Note that this repository is primarily for educational purposes and not designed as a standalone library for direct import into projects.

Competitor Comparisons

96,644

Empowering everyone to build reliable and efficient software.

Pros of rust

  • Official Rust programming language repository, containing the compiler, standard library, and documentation
  • Extensive community support and regular updates from the Rust core team
  • Comprehensive test suite and CI/CD pipeline for ensuring language stability

Cons of rust

  • Large codebase with complex architecture, making it challenging for newcomers to contribute
  • Focused on language implementation rather than providing algorithm examples
  • Steeper learning curve for those interested in understanding Rust internals

Code Comparison

rust:

pub fn add(left: usize, right: usize) -> usize {
    left + right
}

TheAlgorithms/Rust:

pub fn bubble_sort<T: Ord>(arr: &mut [T]) {
    for i in 0..arr.len() {
        for j in 0..arr.len() - 1 - i {
            if arr[j] > arr[j + 1] {
                arr.swap(j, j + 1);
            }
        }
    }
}

The rust repository focuses on core language implementation, while TheAlgorithms/Rust provides algorithm implementations in Rust. The code examples showcase this difference, with rust featuring a simple addition function and TheAlgorithms/Rust demonstrating a bubble sort algorithm.

48,187

ripgrep recursively searches directories for a regex pattern while respecting your gitignore

Pros of ripgrep

  • Highly optimized for performance, making it significantly faster than traditional search tools
  • Supports searching in compressed files and archives without extraction
  • Automatically respects .gitignore rules, simplifying searches in project directories

Cons of ripgrep

  • Focused on a single task (searching), while Rust provides a broader range of algorithms
  • Less educational value for those learning Rust, as it's a production-ready tool rather than a learning resource
  • May have a steeper learning curve for users unfamiliar with command-line tools

Code Comparison

ripgrep:

pub fn search<R: io::Read>(reader: R) -> io::Result<()> {
    let mut buf_reader = io::BufReader::new(reader);
    let mut line = String::new();
    while buf_reader.read_line(&mut line)? > 0 {
        // Search logic here
    }
}

Rust:

pub fn binary_search<T: Ord>(arr: &[T], target: &T) -> Option<usize> {
    let mut left = 0;
    let mut right = arr.len();
    while left < right {
        let mid = left + (right - left) / 2;
        // Binary search logic here
    }
}

The code snippets demonstrate the different focus of each repository. ripgrep is optimized for file searching, while Rust provides implementations of various algorithms like binary search.

33,285

A simple, fast and user-friendly alternative to 'find'

Pros of fd

  • Focused, production-ready tool for finding files and directories
  • Optimized for performance and user-friendly command-line interface
  • Actively maintained with frequent updates and bug fixes

Cons of fd

  • Limited scope compared to the broader algorithm collection in Rust
  • May not be as educational for learning various algorithms and data structures
  • Fewer contributors and less diverse codebase

Code Comparison

fd:

pub fn run() -> Result<()> {
    let config = Config::from_args();
    let walker = FileWalker::new(&config)?;
    walker.run()
}

Rust:

pub fn binary_search<T: Ord>(arr: &[T], target: &T) -> Option<usize> {
    let mut left = 0;
    let mut right = arr.len();
    while left < right {
        let mid = left + (right - left) / 2;
        if &arr[mid] == target {
            return Some(mid);
        } else if &arr[mid] < target {
            left = mid + 1;
        } else {
            right = mid;
        }
    }
    None
}

Summary

fd is a specialized tool for file searching, while Rust is a comprehensive collection of algorithms. fd offers better performance and usability for its specific use case, but Rust provides a wider range of educational resources for learning various algorithms and data structures in Rust programming language.

48,640

A cat(1) clone with wings.

Pros of bat

  • Focused, production-ready tool for enhanced file viewing
  • Extensive features like syntax highlighting, Git integration, and paging
  • Well-documented with clear installation and usage instructions

Cons of bat

  • Limited scope compared to the broad algorithm collection in Rust
  • May require more system resources for its advanced features
  • Less educational value for learning various algorithms and data structures

Code Comparison

bat (main functionality):

pub fn run() -> Result<()> {
    let config = Config::from_args()?;
    let mut printer = Printer::new(&config);
    printer.print_files()?;
    Ok(())
}

Rust (example algorithm implementation):

pub fn bubble_sort<T: Ord>(arr: &mut [T]) {
    for i in 0..arr.len() {
        for j in 0..arr.len() - 1 - i {
            if arr[j] > arr[j + 1] {
                arr.swap(j, j + 1);
            }
        }
    }
}

Summary

bat is a specialized tool for enhanced file viewing, offering a polished user experience with advanced features. Rust, on the other hand, provides a comprehensive collection of algorithms and data structures, serving as an educational resource for learning Rust programming and computer science concepts. While bat excels in its specific use case, Rust offers broader learning opportunities for aspiring developers and algorithm enthusiasts.

56,041

A cross-platform, OpenGL terminal emulator.

Pros of Alacritty

  • Practical, real-world application as a GPU-accelerated terminal emulator
  • More focused and specific project scope
  • Actively maintained with frequent updates and releases

Cons of Alacritty

  • Less educational value for learning algorithms and data structures
  • More complex codebase, potentially harder for beginners to contribute
  • Narrower focus on terminal emulation, rather than a broad range of algorithms

Code Comparison

Alacritty (main application logic):

fn main() {
    let mut options = Options::default();
    options.parse_cli();
    if let Err(e) = run(options) {
        error!("Error: {}", e);
    }
}

The Algorithms (example algorithm implementation):

pub fn bubble_sort<T: Ord>(arr: &mut [T]) {
    for i in 0..arr.len() {
        for j in 0..arr.len() - 1 - i {
            if arr[j] > arr[j + 1] {
                arr.swap(j, j + 1);
            }
        }
    }
}

The code comparison highlights the difference in focus between the two projects. Alacritty's code is centered around application logic and user interface, while The Algorithms focuses on implementing various algorithms in a clear and educational manner.

44,714

☄🌌️ The minimal, blazing-fast, and infinitely customizable prompt for any shell!

Pros of Starship

  • Practical, real-world application as a customizable shell prompt
  • Active development with frequent updates and releases
  • Extensive documentation and user guides

Cons of Starship

  • Narrower scope, focused on a specific use case
  • Potentially steeper learning curve for non-developers

Code Comparison

Starship (configuration example):

[character]
success_symbol = "[➜](bold green)"
error_symbol = "[✗](bold red)"

[git_branch]
symbol = "🌱 "
truncation_length = 4

The Algorithms (Rust implementation example):

pub fn bubble_sort<T: Ord>(arr: &mut [T]) {
    for i in 0..arr.len() {
        for j in 0..arr.len() - 1 - i {
            if arr[j] > arr[j + 1] {
                arr.swap(j, j + 1);
            }
        }
    }
}

Summary

Starship is a focused project providing a customizable shell prompt, while The Algorithms is a collection of various algorithms implemented in Rust. Starship offers practical utility for developers, while The Algorithms serves as an educational resource for learning about different algorithms and their Rust implementations.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

The Algorithms - Rust

Gitpod Ready-to-Code Build workflow Discord community Gitter chat

All algorithms implemented in Rust - for education

List of Algorithms

See our directory for easier navigation and a better overview of the project.

Contributing

Read through our Contribution Guidelines before you contribute.