Statistics
23
Views
0
Downloads
0
Donations
Support
Share
Uploader

高宏飞

Shared on 2026-01-31

AuthorAarav Joshi

RUST BASICS Programming is the foundation of modern technology, powering everything from smartphones to space exploration. It equips you with problem-solving skills, creativity, and the ability to build solutions that can impact millions. Whether you’re automating tasks, developing software, or diving into cutting-edge fields like AI or blockchain, programming is your gateway to innovation. Why Use Static Compiled Languages? Static compiled languages like Rust, C++, and Go ensure high performance and reliability by catching errors during compilation rather than runtime. Unlike interpreted languages (e.g., Python, JavaScript), compiled languages convert code directly into machine instructions, leading to faster execution. C++ is often used for game development, while Go powers scalable back-end systems. Rust stands out for its emphasis on safety and concurrency, eliminating common bugs like null pointer dereferences and data races without compromising speed.

Tags
No tags
Publisher: 101 Books
Publish Year: 2024
Language: 英文
Pages: 419
File Format: PDF
File Size: 6.5 MB
Support Statistics
¥.00 · 0times
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

(This page has no text content)
Table of Contents Copyright Attribution Recommendation: Disclaimer: Rust Basics Variables and Mutability Control Flow Operators Type Inference and Aliases Exercise: Fibonacci Functions Scopes and Ownership Data Types Overview Numeric Data Types Boolean and Character Types Strings Collections Type Conversion Advanced Data Types and Operators Advanced Operators Arithmetic Operations Type Aliases and Newtypes
Exercise: Data Processing Comparison and Equality Logical Operators Bitwise Operations Compound Types Slices Strings in Detail Ownership in Strings Working with Collections Ownership and Lifetimes Ownership Basics Borrowing Lifetimes Slice Types Move Semantics Drop Trait Exercise: Memory Safety Advanced Lifetimes Collections and Iterators Vectors HashMaps Strings
Iterators collect Exercise: Iterators Error Handling in Iterators Itertools Concurrency and Async Programming Threads Channels Shared State Send and Sync async/await Tokio Exercise: Multi-threaded Application Pitfalls Error Handling Understanding Errors Result and Option Custom Errors Panic Handling Error Propagation Third-Party Libraries Exercise: Error Scenarios
Debugging Testing and Debugging Unit Tests Integration Tests Mocking Compiler Warnings Debugging Tools Performance Testing Exercise: Test-Driven Development Advanced Debugging Advanced Rust Unsafe Rust FFI Macros Custom Derive Memory Management Generics and Traits Exercise: Advanced Features Future of Rust COPYRIGHT
101 Book is a company that makes education affordable and accessible for everyone. They create and sell high-quality books, courses, and learning materials at very low prices to help people around the world learn and grow. Their products cover many topics and are designed for all ages and learning needs. By keeping production costs low without reducing quality, 101 Book helps more people succeed in school and life. Focused on making learning available to everyone, they are changing how education is shared and making knowledge accessible for all. Copyright © 2024 by Aarav Joshi This work is made available under an open-source philosophy. The content of this book may be freely shared, distributed, reproduced, or adapted for any purpose without prior notice or permission from the author. However, as a gesture of courtesy and respect, it is kindly recommended to provide proper attribution to the author and reference this book when utilizing its content. Attribution Recommendation: When sharing or using information from this book, you are encouraged to include the following acknowledgment: “Content derived from a book authored by Aarav Joshi, made open- source for public use.” Disclaimer: This book was collaboratively created with the assistance of artificial intelligence, under the careful guidance and expertise of Aarav Joshi. While every effort has been made to ensure the accuracy and
reliability of the content, readers are encouraged to verify information independently for specific applications or use cases. Thank you for supporting open knowledge sharing. Regards, 101 Books RUST BASICS Programming is the foundation of modern technology, powering everything from smartphones to space exploration. It equips you with problem-solving skills, creativity, and the ability to build solutions that can impact millions. Whether you’re automating tasks, developing software, or diving into cutting-edge fields like AI or blockchain, programming is your gateway to innovation. Why Use Static Compiled Languages? Static compiled languages like Rust, C++, and Go ensure high performance and reliability by catching errors during compilation rather than runtime. Unlike interpreted languages (e.g., Python, JavaScript), compiled languages convert code directly into machine instructions, leading to faster execution. C++ is often used for game development, while Go powers scalable back-end systems. Rust stands out for its emphasis on safety and concurrency, eliminating common bugs like null pointer dereferences and data races without compromising speed. Why Learn Rust?
Rust is the future of systems programming, combining low-level control with high-level safety guarantees. It excels in creating robust applications, from embedded systems to web servers like Actix. Its ownership model ensures memory safety without a garbage collector, making it a favorite for performance-critical industries like finance and gaming. About the Book This book provides a structured guide to mastering Rust, from basics to advanced topics. It’s your comprehensive companion for building efficient, scalable, and secure software with Rust. Let’s get straight onto the lessons without wasting a second. Shall we? Variables and Mutability In Rust, variables play a crucial role in storing and manipulating data. By default, variables in Rust are immutable, which means their values cannot be changed once assigned. This design choice promotes safer and more predictable code, reducing the potential for unexpected side effects and bugs. To declare a variable in Rust, we use the let keyword followed by the variable name and an optional type annotation. If we don’t specify the type, Rust’s type inference system will attempt to deduce it based on the context. Here’s a simple example of variable declaration:
let x = 5; In this case, Rust infers that x is an integer. We can also explicitly specify the type: let y: i32 = 10; Here, we’ve declared y as a 32-bit signed integer. Once a variable is declared, we cannot change its value by default. For instance, the following code will result in a compilation error: let x = 5; x = 10; // Error: cannot assign twice to immutable variable This immutability by default is a key feature of Rust, encouraging developers to think carefully about when and where values should change in their programs. It helps prevent accidental modifications and makes code easier to reason about, especially in concurrent contexts. However, there are situations where we need to modify a variable’s value. In such cases, we can use the mut keyword to declare a mutable variable. Here’s how we can create and modify a mutable variable: let mut z = 15; println!("Initial value of z: {}", z);
z = 20; println!("Updated value of z: {}", z); This code will compile and run successfully, outputting the initial and updated values of z. It’s important to note that even when a variable is declared as mutable, its type cannot be changed. Rust’s strong static typing ensures type consistency throughout a variable’s lifetime. Variables in Rust also have a scope, which is the range of the program for which the variable is valid. Typically, a variable’s scope starts from where it’s declared and continues until the end of the current block (denoted by curly braces). For example: { let inner_var = 42; println!("Inside the block: {}", inner_var); } // println!("Outside the block: {}", inner_var); // This would cause an error In this example, inner_var is only accessible within the block where it’s defined. Rust also allows us to shadow variables, which means we can declare a new variable with the same name as a previous variable. This is often used to change the type of a variable or to perform some transformation on its value. Here’s an example:
let spaces = " "; let spaces = spaces.len(); In this case, we’ve shadowed spaces from a string slice to an integer representing its length. When working with functions, it’s common to pass variables as arguments. By default, passing a variable to a function will move or copy the value, depending on the type. For example: fn main() { let s = String::from("hello"); takes_ownership(s); // println!("{}", s); // This would cause an error as s has been moved let x = 5; makes_copy(x); println!("{}", x); // This is fine, as i32 is Copy } fn takes_ownership(some_string: String) { println!("{}", some_string); } fn makes_copy(some_integer: i32) { println!("{}", some_integer); }
In this example, s is moved into takes_ownership, while x is copied when passed to makes_copy. Understanding variables and mutability is fundamental to writing effective Rust code. By leveraging Rust’s default immutability and carefully choosing when to use mutability, we can write more robust and safer programs. This approach helps prevent common programming errors such as data races and unexpected state changes, while still allowing for efficient and flexible code when needed. As we delve deeper into Rust, we’ll see how these concepts of variables and mutability interact with more advanced features like ownership, borrowing, and lifetimes. These features work together to provide Rust’s unique blend of safety and performance, allowing developers to write fast, concurrent, and memory-safe code without the need for a garbage collector. Remember, while mutability is available when needed, it’s generally a good practice to keep variables immutable unless there’s a specific reason to make them mutable. This approach leads to more predictable and maintainable code, especially as programs grow in complexity. Control Flow Control flow structures in Rust provide developers with powerful tools to manage program execution. These structures include conditional statements, match expressions, and various loop
constructs. Understanding and effectively using these control flow mechanisms is crucial for writing efficient and expressive Rust code. Conditional statements in Rust are primarily implemented using the if expression. Unlike many other programming languages, if in Rust is an expression, not a statement. This means it can return a value, allowing for more concise and expressive code. Here’s a basic example of an if expression: let number = 7; if number < 5 { println!("The number is less than 5"); } else if number == 5 { println!("The number is equal to 5"); } else { println!("The number is greater than 5"); } In this example, the program checks the value of number and executes the appropriate block of code. Rust does not require parentheses around the condition, but the curly braces are mandatory.
Since if is an expression, we can use it to assign values to variables: let condition = true; let number = if condition { 5 } else { 6 }; println!("The value of number is: {}", number); In this case, number will be assigned the value 5. It’s important to note that both arms of the if expression must return the same type. For more complex conditional logic, Rust provides the match expression. match is similar to a switch statement in other languages, but it’s more powerful and expressive. It allows pattern matching against a variety of data structures and ensures that all possible cases are handled. Here’s a simple example: let number = 13; match number { 1 => println!("One"), 2 | 3 | 5 | 7 | 11 => println!("This is a prime"), 13..=19 => println!("A teen"),
_ => println!("Ain't special"), } In this match expression, we check the value of number against several patterns. The | operator allows us to specify multiple values in a single arm. The ..= syntax creates an inclusive range. The _ is a catch-all pattern that matches any value not specified in the previous arms. Rust provides several loop constructs for repeating blocks of code. The most basic is the loop keyword, which creates an infinite loop: loop { println!("This will print forever"); } To break out of a loop, we use the break keyword. We can also return a value from a loop: let result = loop { // Some operation if some_condition { break some_value; } };
For more controlled iteration, Rust offers the while loop: let mut number = 3; while number != 0 { println!("{}!", number); number -= 1; } println!("LIFTOFF!!!"); This loop will continue executing as long as the condition number != 0 is true. For iterating over collections or ranges, Rust provides the for loop: let a = [10, 20, 30, 40, 50]; for element in a.iter() { println!("The value is: {}", element); } The for loop is often used with ranges: for number in 1..4 { println!("{}!", number); } This will print the numbers 1, 2, and 3.
Rust’s control flow structures also interact well with its ownership system. For example, when using a for loop to iterate over a collection, the loop takes ownership of the collection if it’s not explicitly borrowed: let v = vec![1, 2, 3]; for i in v { println!("{}", i); } // println!("{:?}", v); // This would cause an error as v has been moved To avoid this, we can use &v to borrow the vector instead: let v = vec![1, 2, 3]; for i in &v { println!("{}", i); } println!("{:?}", v); // This is fine as v was only borrowed Control flow structures in Rust are designed to be safe and prevent common errors. For
instance, Rust’s match expression must be exhaustive, meaning it must cover all possible cases. This helps prevent bugs that could arise from unhandled cases. These control flow mechanisms, combined with Rust’s ownership system and type safety, allow developers to write robust and efficient code. They provide the flexibility to express complex logic while maintaining the safety guarantees that Rust is known for. As we continue to explore Rust, we’ll see how these control flow structures interact with more advanced features of the language, such as pattern matching with enums, error handling with Result, and working with iterators. These combinations allow for expressive and efficient code that leverages the full power of Rust’s type system and ownership model. Operators Operators in Rust provide a way to perform various operations on data, including arithmetic calculations, comparisons, logical evaluations, and bitwise manipulations. Understanding these operators is crucial for writing efficient and expressive code in Rust. Arithmetic operators in Rust allow for basic mathematical operations. The standard arithmetic operators include addition (+), subtraction
(-), multiplication (*), division (/), and modulus (%). These operators work with both integer and floating-point types: let a = 10; let b = 3; let sum = a + b; // 13 let difference = a - b; // 7 let product = a * b; // 30 let quotient = a / b; // 3 (integer division) let remainder = a % b; // 1 For integer division, Rust truncates towards zero. If you need floating-point division, ensure at least one operand is a float: let float_division = 10.0 / 3.0; // 3.3333333 Rust also provides compound assignment operators that combine an arithmetic operation with assignment: +=, -=, *=, /=, and %=. These operators perform the operation and assign the result to the left operand: let mut x = 5; x += 3; // x is now 8 x *= 2; // x is now 16 Comparison operators in Rust are used to compare values and return a boolean result. The comparison operators include equal to (==), not equal to (!=), less than (<), greater than (>), less than or equal to (<=), and greater than or equal to (>=):
let a = 5; let b = 10; println!("a == b: {}", a == b); // false println!("a != b: {}", a != b); // true println!("a < b: {}", a < b); // true println!("a > b: {}", a > b); // false println!("a <= b: {}", a <= b); // true println!("a >= b: {}", a >= b); // false These operators work with various types, including numbers, characters, and booleans. When comparing floating-point numbers, be cautious of precision issues: let x = 0.1 + 0.2; let y = 0.3; println!("x == y: {}", x == y); // This might be false due to floating-point precision Logical operators in Rust are used to combine boolean expressions. The primary logical operators are AND (&&), OR (||), and NOT (!): let a = true; let b = false; println!("a && b: {}", a && b); // false println!("a || b: {}", a || b); // true println!("!a: {}", !a); // false