Rust
The Practical Guide
Información
- Editorial: SAP PRESS
- Autores: Nouman Azam
- Año: 2025
- Edición: 1
- Páginas: 627
- Idiomas: Inglés
Descripción
Get “close to the machine” by programming with Rust! Discover how to effectively use this low-level language to create fast and efficient code. Set up Rust, compile your first program, and learn the language essentials: variables, functions, conditionals, and more. Walk through Rust’s unique ownership model and modular system, and then move on to more complex features, from flexibility and abstraction to web programming and text processing. Numerous code examples and exercises make this your go-to practical resource for Rust!
- Your hands-on guide to Rust programming
- Work with essential language elements, including variables, strings, collections, functions, structures, and traits
- Explore advanced concepts, including concurrency, testing, and sizing
Aspectos Destacados
- Language basics
- Control flow
- Ownership
- Structs and enums
- Generics and traits
- Modular structure
- Memory management
- Testing
- Concurrency
- Functional programming
- Web programming
- Text processing and file handling
Aprenderás sobre
- Language Basics:
Take your first steps with Rust! After installation and setup in Visual Studio Code, walk through the language building blocks, from variables to functions to enums. Learn how to implement ownership, define types, test your code, and more.
- Intermediate Programming:
Expand your toolkit for Rust programming! Write flexible code with generics and traits, incorporate features of functional programming, manage memory, implement data structures, and handle structs.
- Advanced Topics:
Become a Rust master! Tackle advanced topics, including sized and unsized types, concurrency via threading, the powerful macro system, web programming, text processing, and file and directory handling.
Tabla de Contenidos
- Preface
- The Purpose of This Book
- The Structure of This Book
- Target Audience
- Acknowledgments
- PART I Basic Programming with Rust
- 1 Introduction
- 1.1 Installing Rust and Its Web-Based Environment
- 1.1.1 Installing Rust
- 1.1.2 Rust’s Web-Based Compiler
- 1.2 Running and Compiling Your First Program
- 1.3 Visual Studio Code Settings
- 1.4 Making the Most of This Book
- 1.5 Summary
- 2 Variables, Data Types, and Functions
- 2.1 Variables
- 2.1.1 Definition
- 2.1.2 Mutability of Variables
- 2.1.3 Scope of Variables
- 2.1.4 Shadowing
- 2.1.5 Constants
- 2.1.6 Statics
- 2.1.7 Unused Variables
- 2.2 Data Types
- 2.2.1 Primitive Data Types
- 2.2.2 Compound Data Types
- 2.2.3 Text-Related Types
- 2.3 Functions
- 2.4 Code Blocks
- 2.5 Practice Exercises
- 2.6 Solutions
- 2.7 Summary
- 3 Conditionals and Control Flow
- 3.1 Conditionals
- 3.1.1 If Else
- 3.1.2 If Else If Ladder
- 3.1.3 Match
- 3.2 Control Flow
- 3.2.1 Simple Loops
- 3.2.2 For and While Loops
- 3.3 Comments, Outputs, and Inputs
- 3.3.1 Comments
- 3.3.2 Formatting Outputs with Escape Sequences and Arguments
- 3.3.3 User Input
- 3.4 Practice Exercises
- 3.5 Solutions
- 3.6 Summary
- 4 Ownership
- 4.1 Ownership Basics
- 4.1.1 Values Must Have a Single Owner
- 4.1.2 When Owner Goes Out of Scope, the Value Is Cleaned Up
- 4.2 Ownership in Functions
- 4.2.1 Functions Taking Ownership
- 4.2.2 Function Returning Ownership
- 4.2.3 Function Taking and Returning Ownership
- 4.3 Borrowing Basics
- 4.3.1 Why Borrowing?
- 4.3.2 Borrowing Rules
- 4.3.3 Copying of References
- 4.4 Borrowing in Functions
- 4.5 Dereferencing
- 4.6 Mutable and Immutable Binding of References
- 4.7 Practice Exercises
- 4.8 Solutions
- 4.9 Summary
- 5 Custom and Library-Provided Useful Types
- 5.1 Structs
- 5.1.1 Defining Structs
- 5.1.2 Instantiating Struct Instances
- 5.1.3 Ownership Considerations
- 5.1.4 Tuple Structs
- 5.1.5 Unit Structs
- 5.1.6 Adding Functionality to Structs
- 5.1.7 Associated Functions
- 5.2 Enums
- 5.2.1 Why Use Enums?
- 5.2.2 Defining Enums
- 5.2.3 Implementation Blocks for Enums
- 5.2.4 Adding Data to Enum Variants
- 5.3 Option
- 5.3.1 Why Use Option?
- 5.3.2 Defining the Option Enum
- 5.3.3 Matching on Option
- 5.3.4 Use of If Let with Option
- 5.4 Result
- 5.4.1 Why Use Result?
- 5.4.2 Defining the Result Enum
- 5.4.3 Matching on Result
- 5.4.4 The ? Operator
- 5.5 HashMaps
- 5.6 HashSets
- 5.7 Practice Exercises
- 5.8 Solutions
- 5.9 Summary
- 6 Organizing Your Code
- 6.1 Code Organization
- 6.2 Module Basics
- 6.2.1 Motivating Example for Modules
- 6.2.2 Creating Modules
- 6.2.3 Relative and Absolute Paths of Items
- 6.2.4 Privacy in Modules
- 6.2.5 The Use Declaration for Importing or Bringing Items into Scope
- 6.3 Visualizing and Organizing Modules
- 6.3.1 Cargo Modules for Visualizing Module Hierarchy
- 6.3.2 Organizing Code Using a Typical File System
- 6.4 Re-Exporting and Privacy
- 6.4.1 Re-Exporting with a Pub Use Expression
- 6.4.2 Privacy of Structs
- 6.5 Using External Dependencies
- 6.6 Publishing Your Crate
- 6.6.1 Creating an Account on crates.io
- 6.6.2 Adding Documentation before Publishing
- 6.6.3 Publishing Your Crate
- 6.7 Practice Exercises
- 6.8 Solutions
- 6.9 Summary
- 7 Testing Code
- 7.1 Unit Testing
- 7.1.1 A Typical Test Case
- 7.1.2 Writing a Test Function
- 7.1.3 Executing Tests
- 7.1.4 Testing with Result Enum
- 7.1.5 Testing Panics
- 7.2 Controlling Test Execution
- 7.3 Integration Tests
- 7.4 Benchmark Testing
- 7.5 Practice Exercises
- 7.6 Solutions
- 7.7 Summary
- PART II Intermediate Language Concepts
- 8 Flexibility and Abstraction with Generics and Traits
- 8.1 Generics
- 8.1.1 Basics of Generics
- 8.1.2 Generics in Implementation Blocks
- 8.1.3 Multiple Implementations for a Type: Generics versus Concrete
- 8.1.4 Duplicate Definitions in Implementation Blocks
- 8.1.5 Generics and Free Functions
- 8.1.6 Monomorphization
- 8.2 Traits
- 8.2.1 Motivating Example for Traits
- 8.2.2 Traits Basics
- 8.2.3 Default Implementations
- 8.2.4 Trait Bounds
- 8.2.5 Supertraits
- 8.2.6 Trait Objects
- 8.2.7 Derived Traits
- 8.2.8 Marker Traits
- 8.2.9 Associated Types in Traits
- 8.3 Choosing between Associated Types and Generic Types
- 8.4 Practice Exercises
- 8.5 Solutions
- 8.6 Summary
- 9 Functional Programming Aspects
- 9.1 Closures
- 9.1.1 Motivating Example for Closures
- 9.1.2 Basic Syntax
- 9.1.3 Passing Closures to Functions
- 9.1.4 Capturing Variables from the Environment
- 9.2 Function Pointers
- 9.3 Iterators
- 9.3.1 The Iterator Trait
- 9.3.2 IntoIterator
- 9.3.3 Iterating over Collections
- 9.4 Combinators
- 9.5 Iterating through Option
- 9.6 Practice Exercises
- 9.7 Solutions
- 9.8 Summary
- 10 Memory Management Features
- 10.1 Lifetimes
- 10.1.1 Concrete Lifetimes
- 10.1.2 Generic Lifetimes
- 10.1.3 Static Lifetimes
- 10.1.4 Lifetime Elision
- 10.1.5 Lifetimes and Structs
- 10.2 Smart Pointers
- 10.2.1 Box Smart Pointer
- 10.2.2 Rc Smart Pointer
- 10.2.3 RefCell Smart Pointer
- 10.3 Deref Coercion
- 10.4 Practice Exercises
- 10.5 Solutions
- 10.6 Summary
- 11 Implementing Typical Data Structures
- 11.1 Singly Linked List
- 11.1.1 Implementation by Modifying the List Enum
- 11.1.2 Resolving Issues with the Implementation
- 11.1.3 Refining the Next Field
- 11.1.4 Adding Elements
- 11.1.5 Removing Elements
- 11.1.6 Printing Singly Linked Lists
- 11.2 Doubly Linked List
- 11.2.1 Setting Up the Basic Data Structure
- 11.2.2 Adding Elements
- 11.2.3 Adding a New Constructor Function for Node
- 11.2.4 Removing Elements
- 11.2.5 Printing Doubly Linked Lists
- 11.3 Reference Cycles Creating Memory Leakage
- 11.4 Practice Exercises
- 11.5 Solutions
- 11.6 Summary
- 12 Useful Patterns for Handling Structs
- 12.1 Initializing Struct Instances
- 12.1.1 New Constructors
- 12.1.2 Default Constructors
- 12.2 Builder Pattern
- 12.2.1 Motivating Example for the Builder Pattern
- 12.2.2 Solving the Proliferation of Constructors
- 12.3 Simplifying Structs
- 12.4 Practice Exercises
- 12.5 Solutions
- 12.6 Summary
- PART III Advanced Language Concepts
- 13 Understanding Size in Rust
- 13.1 Sized and Unsized Types
- 13.1.1 Examples of Sized Types
- 13.1.2 Examples of Unsized Types
- 13.2 References to Unsized Types
- 13.3 Sized and Optionally Sized Traits
- 13.3.1 Opting Out of Sized Trait
- 13.3.2 Generic Bound of Sized Traits
- 13.3.3 Flexible Generic Function with Optionally Sized Trait
- 13.4 Unsized Coercion
- 13.4.1 Deref Coercion
- 13.4.2 Unsized Coercion
- 13.4.3 Unsized Coercion with Traits
- 13.5 Zero-Sized Types
- 13.5.1 Never Type
- 13.5.2 Unit Type
- 13.5.3 Unit Structs
- 13.5.4 PhantomData
- 13.6 Practice Exercises
- 13.7 Solutions
- 13.8 Summary
- 14 Concurrency
- 14.1 Thread Basics
- 14.1.1 Fundamental Concepts
- 14.1.2 Creating Threads
- 14.1.3 Thread Completion Using Sleep
- 14.1.4 Thread Completion Using Join
- 14.2 Ownership in Threads
- 14.3 Thread Communication
- 14.3.1 Message Passing
- 14.3.2 Sharing States
- 14.4 Synchronization through Barriers
- 14.4.1 Motivating Example for Barriers
- 14.4.2 Synchronizing Threads Using Barriers
- 14.5 Scoped Threads
- 14.6 Thread Parking
- 14.6.1 Motivating Example for Thread Parking
- 14.6.2 Temporarily Blocking a Thread
- 14.6.3 Park Timeout Function
- 14.7 Async Await
- 14.7.1 Creating Async Functions
- 14.7.2 Deriving Futures to Completion with the Await Method
- 14.7.3 Tokio
- 14.8 Web Scraping Using Threads
- 14.9 Practice Exercises
- 14.10 Solutions
- 14.11 Summary
- 15 Macros
- 15.1 Macro Basics
- 15.1.1 Basic Syntax
- 15.1.2 Matching Pattern in the Rule
- 15.1.3 Captures
- 15.1.4 Strict Matching
- 15.1.5 Macro Expansion
- 15.2 Capturing Types
- 15.2.1 Type Capture
- 15.2.2 Identifiers Capture
- 15.3 Repeating Patterns
- 15.4 Practice Exercises
- 15.5 Solutions
- 15.6 Summary
- 16 Web Programming
- 16.1 Creating a Server
- 16.1.1 Server Basics
- 16.1.2 Implementing a Server
- 16.1.3 Handling Multiple Connections
- 16.1.4 Adding a Connection Handling Function
- 16.2 Making Responses
- 16.2.1 Response Syntax
- 16.2.2 Responding with Valid HTML
- 16.2.3 Returning Different Responses
- 16.3 Multithreaded Server
- 16.4 Practice Exercises
- 16.5 Solutions
- 16.6 Summary
- 17 Text Processing, File Handling, and Directory Management
- 17.1 Basic File Handling
- 17.1.1 Creating a File and Adding Content
- 17.1.2 Appending a File
- 17.1.3 Storing the Results
- 17.1.4 Reading from a File
- 17.2 Path- and Directory-Related Functions
- 17.2.1 Working with Paths
- 17.2.2 Working with Directories
- 17.3 Regular Expressions Basics
- 17.3.1 Basic Methods
- 17.3.2 Dot and Character Ranges
- 17.3.3 Starting and Ending Anchors
- 17.3.4 Word Boundaries
- 17.3.5 Quantifiers, Repetitions, and Capturing Groups
- 17.4 String Literals
- 17.4.1 Working with Raw String Literals
- 17.4.2 Parsing JSON
- 17.4.3 Using a Hash within a String
- 17.5 Practice Exercises
- 17.6 Solutions
- 17.7 Summary
- 18 Practical Problems
- 18.1 Problem 1: Search Results with Word Groupings
- 18.1.1 Solution Setup
- 18.1.2 Implementation
- 18.2 Problem 2: Product Popularity
- 18.2.1 Solution Setup
- 18.2.2 Implementation
- 18.3 Problem 3: Highest Stock Price
- 18.3.1 Solution Setup
- 18.3.2 Implementation
- 18.4 Problem 4: Identify Time Slots
- 18.4.1 Solution Setup
- 18.4.2 Implementation
- 18.5 Problem 5: Item Suggestions
- 18.5.1 Solution Setup
- 18.5.2 Implementation
- 18.6 Problem 6: Items in Range Using Binary Search Trees
- 18.6.1 Solution Setup
- 18.6.2 Basic Data Structure
- 18.6.3 Implementation
- 18.7 Problem 7: Fetching Top Products
- 18.7.1 Solution Setup
- 18.7.2 Implementation
- 18.8 Problem 8: Effective Storage and Retrieval
- 18.8.1 Solution Setup
- 18.8.2 Basic Data Structure
- 18.8.3 Implementation
- 18.9 Problem 9: Most Recently Used Product
- 18.9.1 Solution Setup
- 18.9.2 Basic Data Structure
- 18.9.3 Implementation
- 18.10 Problem 10: Displaying Participants in an Online Meeting
- 18.10.1 Solution Setup
- 18.10.2 Basic Data Structure
- 18.10.3 Implementation
- 18.11 Summary
Descargo de responsabilidad
SAP, otros productos SAP y servicios mencionados aquí así como sus respectivos logos son marca registrada de SAP SE (o una compañía afiliada de SAP) en Alemania y otros países. Nuestra compañía no está afiliada con SAP SE ni con ninguna de sus compañías afiliadas incluyendo pero no limitada a: Sybase, Business Objects, Hybris, Ariba y SuccessFactors. Todos los otros nombres, marcas, logos, etc. son marcas o servicios registrados de sus respectivos propietarios.