Convert Figma logo to code with AI

rust-lang logorust-by-example

Learn Rust with examples (Live code editor included)

6,966
1,340
6,966
64

Top Related Projects

14,881

The Rust Programming Language

52,586

:crab: Small exercises to get you used to reading and writing Rust code!

22,076

All Algorithms implemented in Rust

A curated list of Rust code and resources.

96,644

Empowering everyone to build reliable and efficient software.

A guide to how rustc works and how to contribute to it.

Quick Overview

Rust by Example (RBE) is an official Rust project that provides a hands-on introduction to Rust through annotated example programs. It covers a wide range of Rust concepts, from basic syntax to advanced features, allowing users to learn by running and modifying real code examples.

Pros

  • Interactive learning experience with runnable code examples
  • Comprehensive coverage of Rust language features and concepts
  • Regularly updated to reflect the latest Rust language changes
  • Well-organized structure, making it easy to navigate and learn progressively

Cons

  • May not be as in-depth as a full textbook or course
  • Some examples might be too concise for complete beginners
  • Lacks exercises or challenges for reinforcing learned concepts
  • Navigation can be overwhelming due to the large number of topics covered

Code Examples

Since Rust by Example is not a code library but an educational resource, we'll skip the code examples section. However, here's a brief overview of how the examples are structured in the project:

// This is a comment, and is ignored by the compiler

// This is the main function
fn main() {
    // Statements here are executed when the compiled binary is called

    // Print text to the console
    println!("Hello World!");
}

Each example is accompanied by explanations and can be run directly in the browser or locally.

Getting Started

To get started with Rust by Example:

  1. Visit the official website: https://doc.rust-lang.org/rust-by-example/
  2. Browse through the table of contents on the left sidebar
  3. Click on a topic to view the example and explanation
  4. Use the "Run" button to execute the code in your browser
  5. Modify the code to experiment and learn

For a local copy:

  1. Clone the repository: git clone https://github.com/rust-lang/rust-by-example.git
  2. Navigate to the project directory: cd rust-by-example
  3. Build the book: mdbook build
  4. Open book/index.html in your web browser to view the content locally

Competitor Comparisons

14,881

The Rust Programming Language

Pros of book

  • More comprehensive and in-depth coverage of Rust concepts
  • Structured learning path with progressive difficulty
  • Includes exercises and quizzes for better retention

Cons of book

  • Less hands-on coding examples compared to rust-by-example
  • May be overwhelming for beginners looking for quick reference
  • Takes longer to work through the entire content

Code Comparison

book:

fn main() {
    let x = 5;
    let y = {
        let x = 3;
        x + 1
    };
    println!("The value of y is: {}", y);
}

rust-by-example:

fn main() {
    let x = 5u32;
    let y = {
        let x_squared = x * x;
        let x_cube = x_squared * x;
        x_cube + x_squared + x
    };
    println!("x is {:?}", x);
    println!("y is {:?}", y);
}

The book example focuses on explaining scopes and shadowing, while rust-by-example demonstrates more complex expressions and formatting in a single example.

rust-by-example provides concise, runnable code snippets for quick reference and experimentation. The book offers a more structured approach with detailed explanations and gradual concept introduction. Both repositories complement each other, catering to different learning styles and needs within the Rust community.

52,586

:crab: Small exercises to get you used to reading and writing Rust code!

Pros of Rustlings

  • Interactive, hands-on learning experience with exercises
  • Automated testing and feedback for immediate validation
  • Gradual progression from basic to advanced concepts

Cons of Rustlings

  • Less comprehensive coverage of Rust features
  • Requires local setup and command-line interaction
  • May be overwhelming for absolute beginners

Code Comparison

Rustlings exercise example:

// exercises/variables/variables2.rs
fn main() {
    let x;
    if x == 10 {
        println!("x is ten!");
    } else {
        println!("x is not ten!");
    }
}

Rust by Example code snippet:

// variables/mut.md
fn main() {
    let mut x = 5;
    println!("The value of x is: {}", x);
    x = 6;
    println!("The value of x is: {}", x);
}

Rust by Example provides explanations alongside code, while Rustlings focuses on interactive problem-solving. Both repositories offer valuable resources for learning Rust, with Rustlings emphasizing hands-on practice and Rust by Example providing a more comprehensive reference. Rust by Example covers a wider range of topics and is better suited for exploration, while Rustlings offers a structured learning path with immediate feedback. The choice between the two depends on individual learning preferences and goals.

22,076

All Algorithms implemented in Rust

Pros of The Algorithms

  • Focuses on practical implementation of various algorithms in Rust
  • Provides a wide range of algorithm categories (e.g., sorting, searching, data structures)
  • Serves as a valuable resource for learning algorithm design and Rust simultaneously

Cons of The Algorithms

  • Less structured learning path compared to Rust by Example
  • May lack detailed explanations for each algorithm implementation
  • Not officially maintained by the Rust team, potentially leading to outdated code

Code Comparison

Rust by Example (Simple array iteration):

fn main() {
    let array = [1, 2, 3, 4, 5];
    for x in array.iter() {
        println!("{}", x);
    }
}

The Algorithms (Binary search implementation):

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 {
            left = mid + 1;
        } else {
            right = mid;
        }
    }
    None
}

The code comparison showcases the difference in focus between the two repositories. Rust by Example provides simple, easy-to-understand code snippets for learning Rust syntax and concepts, while The Algorithms offers more complex implementations of specific algorithms.

A curated list of Rust code and resources.

Pros of awesome-rust

  • Comprehensive collection of Rust resources, libraries, and tools
  • Regularly updated with community contributions
  • Covers a wide range of topics and use cases

Cons of awesome-rust

  • Less structured learning path for beginners
  • Lacks in-depth explanations and code examples
  • May be overwhelming due to the sheer amount of information

Code comparison

rust-by-example:

fn main() {
    println!("Hello, world!");
}

awesome-rust: No direct code examples, but links to various projects and libraries.

Summary

rust-by-example is an official Rust project that provides a structured, hands-on approach to learning Rust through annotated examples. It's ideal for beginners and those who prefer a step-by-step learning process.

awesome-rust is a community-driven curated list of Rust resources, libraries, and tools. It's more suitable for experienced developers looking for specific solutions or exploring the Rust ecosystem.

While rust-by-example focuses on teaching Rust concepts through code examples, awesome-rust serves as a comprehensive directory of Rust-related resources. Both repositories complement each other, catering to different needs within the Rust community.

96,644

Empowering everyone to build reliable and efficient software.

Pros of rust

  • Comprehensive source code for the Rust compiler and standard library
  • Includes extensive test suites and documentation
  • Allows for deeper understanding of Rust's internals and implementation details

Cons of rust

  • Large and complex codebase, potentially overwhelming for beginners
  • Requires more time and effort to navigate and understand
  • May not be as focused on learning Rust syntax and concepts

Code comparison

rust:

pub fn compile_crate(sess: &Session, crate_name: &str) -> Result<(), ErrorReported> {
    let (krate, lint_store, mut sess) = front_end(sess, crate_name)?;
    let (outputs, _) = back_end(&mut sess, &krate, &lint_store)?;
    sess.finish_outputs(outputs)
}

rust-by-example:

fn main() {
    println!("Hello, world!");
    let x = 5;
    let y = 10;
    println!("x + y = {}", x + y);
}

The rust repository contains more complex, compiler-related code, while rust-by-example focuses on simple, educational examples for learning Rust syntax and concepts.

A guide to how rustc works and how to contribute to it.

Pros of rustc-dev-guide

  • Provides in-depth information about the Rust compiler's internals
  • Offers guidance for contributing to the Rust project
  • Includes detailed explanations of compiler architecture and implementation

Cons of rustc-dev-guide

  • More complex and less beginner-friendly
  • Focuses on compiler development rather than general Rust programming
  • May be overwhelming for those not interested in compiler internals

Code comparison

rustc-dev-guide (compiler-specific example):

fn main() {
    let result = rustc_interface::run_compiler(config, |compiler| {
        compiler.enter(|queries| {
            queries.global_ctxt().unwrap().enter(|tcx| {
                // Compiler-specific operations
            })
        })
    });
}

rust-by-example (general Rust example):

fn main() {
    let x = 5;
    let y = 10;
    println!("x + y = {}", x + y);
}

The rustc-dev-guide focuses on compiler-specific code and concepts, while rust-by-example provides general Rust programming examples. The rustc-dev-guide is more suitable for those interested in contributing to the Rust compiler or understanding its internals, whereas rust-by-example is better for learning Rust programming basics and common patterns.

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

Rust By Example

Build Status

Learn Rust with examples (Live code editor included)

Using

If you'd like to read Rust by Example, you can visit https://doc.rust-lang.org/rust-by-example/ to read it online.

If you'd like to read it locally, install Rust, and then:

git clone https://github.com/rust-lang/rust-by-example
cd rust-by-example
cargo install mdbook
mdbook build
mdbook serve

To be able to run the examples, you must be connected to the internet; you can read all content offline, however!

The following warnings can be ignored safely.

[WARN] (mdbook::preprocess::cmd): The command wasn't found, is the "gettext" preprocessor installed?
[WARN] (mdbook::preprocess::cmd):   Command: mdbook-gettext

Using translated version

If there is a translated resource in po/ directory, it can be specified through MDBOOK_BOOK__LANGUAGE like below:

git clone https://github.com/rust-lang/rust-by-example
cd rust-by-example
cargo install mdbook
MDBOOK_BOOK__LANGUAGE=ja mdbook build
MDBOOK_BOOK__LANGUAGE=ja mdbook serve

Contributing

Please see the CONTRIBUTING.md file for more details.

Translating

Please see the TRANSLATING.md file for more details.

Translating guide for each languages

Translations to other languages

License

Rust by Example is licensed under either of

at your option.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in Rust by Example by you, as defined in the Apache-2.0 license, shall be dually licensed as above, without any additional terms or conditions.