NexZap - Home

Rust

What is Rust?

Rust is an empowering programming language designed to give you high performance and reliability through ironclad safety. You can build almost anything, from blazing-fast web servers to intricate system tools, with stellar performance. When your code finally compiles, it’s rock-solid. The catch? Getting it to compile can feel like wrestling a bear, it’s tough but rewarding.

fn reverse_string(input: &str) -> String {
    let mut result = String::new();
    for c in input.chars().rev() {
        result.push(c);
    }
    result
}

fn main() {
    let text = "hello";
    let reversed = reverse_string(text);
    // "Original: hello, Reversed: olleh"
    println!("Original: {}, Reversed: {}", text, reversed);
}

Key Features

- Memory Safety: Ownership model ensures safe memory management at compile time, matching C++ speed without crashes or garbage collection.
- Zero-Cost Abstractions: Pattern matching and iterators compile to lean code, adding expressiveness with no runtime cost.
- Strong Type System: Strict typing catches errors like null pointers and data races early, reducing bugs.
- Safe Concurrency: Borrow checker prevents data races, ideal for multithreaded applications.
- Rich Ecosystem: cargo manages builds and dependencies; crates.io provides diverse libraries.

Pros

- Safety First: Compile-time checks eliminate bugs like null pointers and buffer overflows, great for systems programming.
- Performance: Matches C++ speed, ideal for game engines and embedded systems.
- Modern Syntax: Expressive features like pattern matching blend low-level control with high-level ease, though type system can be complex.
- Tooling: cargo, rustfmt, and clippy streamline project setup, testing, and code quality.
- Growing Community: Backed by Mozilla, adopted by Microsoft and AWS, with a growing ecosystem.

Cons

- Steep Learning Curve: Borrow checker and ownership rules can frustrate newcomers with complex compile errors.
- Verbosity: Safety requires explicit annotations, making code wordier than Python or Go.
- Slow Compilation and big target: Large projects or complex targets can lead to lengthy compile times.
- Young Ecosystem: Less mature than Java or Python, sometimes requiring custom solutions.

Why Learn Rust?

Rust is your pick for building fast, reliable systems with ironclad safety—like crafting a race car with airbags. It’s ideal for system-level programming, web backends where saftery or performance is critical, and game engine development, but less suited for game development due to slow iteration cycles (a great article to read Leaving Rust gamedev after 3 years). Rust isn’t a kind buddy holding your hand; it’s like a stern dad who pushes you hard until your work is flawless. You might want to quit, but if you persevere, you’ll build unbreakable software. Ready to tame the bear? Rust equips you to create programs that endure.

Page 1 of 5

Your Solution

Task: Build a String Length Calculator

In this exercise, you’ll create a function to calculate the length of a string without using iterators, showcasing Rust’s string handling and type safety.

Instructions

Write a function string_length(input: &str) -> usize that takes a string slice and returns its length by counting characters manually.

Example:

- Input: "hello"
- Output: 5