Discover the powerful, hidden features of Rust you need to build robust, concurrent, and fast applicationsAbout This Book\* Learn how concurrency works in Rust and why it is safe\* Get to know the different philosophies of error handling and how to use them wisely\* After reading this book, you will be able to migrate your legacy C or C++ application to a Rust environmentWho This Book Is ForThe target audience would be readers having knowledge of other programming languages and are able to work fluently in the operating system of their choice, be it Linux, OS X or Windows. Since Rust is a rather new language, they are interested in programming beyond simply using it for work. The book focuses on intermediate and advanced features of Rust. What You Will Learn\* Implement unit testing patterns with the standard Rust tools\* Get to know the different philosophies of error handling and how to use them wisely\* Appreciate Rust's ability to solve memory allocation problems safely without garbage collection\* Get to know how concurrency works in Rust and use concurrency primitives such as threads and message passing\* Use syntax extensions and write your own\* Create a Web application with Rocket\* Use Diesel to build safe database abstractionsIn DetailIf concurrent programs are giving you sleepless nights, Rust is your go-to language. Being one of the first ever comprehensive books on Rust, it is filled with real-world examples and explanations, showing you how you can build scalable and reliable programs for your organization. We'll teach you intermediate to advanced level concepts that make Rust a great language. Improving performance, using generics, building macros, and working with threads are just some of the topics we'll cover. We'll talk about the official toolsets and ways to discover more. The book contains a mix of theory interspersed with hands-on tasks, so you acquire the skills as well as the knowledge. Since programming cannot be learned by just reading, we provide exercises (and solutions) to hammer the concepts in. After reading this book, you will be able to implement Rust for your enterprise project, deploy the software, and will know the best practices of coding in Rust. Style and approachThis book is your one stop guide to the Rust programming language and covers advanced-level concepts in a detailed manner using real-world examples. Read more... Abstract: Discover the powerful, hidden features of Rust you need to build robust, concurrent, and fast applicationsAbout This Book\* Learn how concurrency works in Rust and why it is safe\* Get to know the different philosophies of error handling and how to use them wisely\* After reading this book, you will be able to migrate your legacy C or C++ application to a Rust environmentWho This Book Is ForThe target audience would be readers having knowledge of other programming languages and are able to work fluently in the operating system of their choice, be it Linux, OS X or Windows. Since Rust is a rather new language, they are interested in programming beyond simply using it for work. The book focuses on intermediate and advanced features of Rust. What You Will Learn\* Implement unit testing patterns with the standard Rust tools\* Get to know the different philosophies of error handling and how to use them wisely\* Appreciate Rust's ability to solve memory allocation problems safely without garbage collection\* Get to know how concurrency works in Rust and use concurrency primitives such as threads and message passing\* Use syntax extensions and write your own\* Create a Web application with Rocket\* Use Diesel to build safe database abstractionsIn DetailIf concurrent programs are giving you sleepless nights, Rust is your go-to language. Being one of the first ever comprehensive books on Rust, it is filled with real-world examples and explanations, showing you how you can build scalable and reliable programs for your organization. We'll teach you intermediate to advanced level concepts that make Rust a great language. Improving performance, using generics, building macros, and working with threads are just some of the topics we'll cover. We'll talk about the official toolsets and ways to discover more. The book contains a mix of theory interspersed with hands-on tasks, so you acquire the skills as well as the knowledge. Since programming cannot be learned by just reading, we provide exercises (and solutions) to hammer the concepts in. After reading this book, you will be able to implement Rust for your enterprise project, deploy the software, and will know the best practices of coding in Rust. Style and approachThis book is your one stop guide to the Rust programming language and covers advanced-level concepts in a detailed manner using real-world examples Preface......Page 23 What this book covers......Page 25 What you need for this book......Page 28 Who this book is for......Page 30 Conventions......Page 32 Reader feedback......Page 34 Customer support......Page 36 Downloading the example code......Page 38 Errata......Page 40 Piracy......Page 42 Questions......Page 44 Getting Your Feet Wet......Page 46 What is Rust and why should you care?......Page 48 Installing Rust compiler and Cargo......Page 50 Using rustup.rs......Page 52 A tour of the language and trying it out......Page 55 Constants and variables......Page 58 Loops......Page 61 Compound data......Page 63 Enums and pattern matching......Page 67 Struct methods......Page 70 Using other pieces of code in your module......Page 73 Sequences......Page 75 Exercise - fix the word counter......Page 78 Summary......Page 81 Using Cargo to Build Your First Program......Page 83 Cargo and crates......Page 85 Founding a project - cargo init......Page 88 Dependencies, building, and running......Page 91 Running tests - cargo test......Page 94 Cargo.toml - project metadata......Page 98 Editor integrations......Page 101 Final exercise - starting our project......Page 108 Summary......Page 110 Unit Testing and Benchmarking......Page 112 Motivation and high-level view......Page 114 Annotations......Page 116 Assert macros......Page 120 Integration or black box tests......Page 122 Documentation tests......Page 124 Benchmarks......Page 127 Integrating with Travis......Page 130 Founding a city-builder game......Page 134 Final exercise - fixing the tests......Page 139 Summary......Page 141 Types......Page 143 String types......Page 145 String slices......Page 147 The String type......Page 150 Byte strings......Page 153 Takeaways and tasks......Page 155 Arrays and slices......Page 157 Takeaways and tasks......Page 160 Generic types......Page 162 Takeaways and tasks......Page 165 Traits and implementations......Page 167 Takeaways and tasks......Page 173 Constants and statics......Page 175 Summary......Page 177 Error Handling......Page 179 Option and Result......Page 181 Unwrapping......Page 185 Mapping of the Option/Result values......Page 187 Early returns and the try! macro......Page 190 The ? operator......Page 193 Panicking......Page 195 Custom errors and the Error trait......Page 198 Exercise......Page 203 Summary......Page 206 Memory, Lifetimes, and Borrowing......Page 208 LLVM......Page 210 Function variables and the stack......Page 215 The heap......Page 220 Memory safety......Page 223 Ownership......Page 226 Copy trait......Page 229 Function parameters and patterns......Page 232 Borrowing......Page 235 Lifetimes......Page 240 Globals......Page 242 References as function parameters......Page 244 Structs and struct fields......Page 246 Impl signatures......Page 248 The Drop trait......Page 250 Collector types......Page 252 Box ......Page 254 Interior mutability for Copy types - Cell ......Page 256 Interior mutability for move types - RefCell ......Page 259 Practical uses of interior mutability......Page 263 Reference collected memory: Rc and Arc ......Page 266 Inspecting memory usage with std::mem......Page 271 Final exercises......Page 273 Summary......Page 275 Concurrency......Page 277 Problems with concurrency......Page 279 Closures......Page 282 Exercises......Page 285 Threads......Page 287 Exercises......Page 290 Sharing the Copy types......Page 292 Channels......Page 294 Exercises......Page 297 Locks and mutexes......Page 299 Exercises......Page 302 Atomic Rc......Page 304 Exercises......Page 308 The final exercise......Page 310 Summary......Page 312 Macros......Page 314 Introduction to metaprogramming......Page 316 Dissecting println!......Page 319 Exercises......Page 323 Debugging macros......Page 325 Macro keywords......Page 330 block......Page 332 expr......Page 334 ident......Page 336 item......Page 338 meta......Page 340 pat......Page 342 path......Page 344 stmt......Page 346 tt......Page 348 ty......Page 350 Repeating constructs......Page 352 Example - an HTTP tester......Page 355 Exercises......Page 358 Summary......Page 360 Compiler Plugins......Page 362 Basics of compiler plugins......Page 364 The minimal compiler plugin......Page 367 Building a compiler plugin via Cargo......Page 370 Code generation as a workaround......Page 372 Aster......Page 375 Linter plugins......Page 378 Macros 1.1 - custom derives......Page 382 Exercises......Page 387 Summary......Page 389 Unsafety and Interfacing with Other Languages......Page 391 Unsafety......Page 393 Calling C code from Rust......Page 397 Connecting external libraries to Rust code......Page 400 Creating Ruby extensions with Ruru......Page 404 JavaScript/Node.js and Neon......Page 409 Exercises......Page 414 Summary......Page 416 Parsing and Serialization......Page 418 Parsing fundamentals......Page 420 nom......Page 423 Chomp......Page 428 Other parser libraries......Page 433 Serde......Page 435 Exercises......Page 441 Summary......Page 443 Web Programming......Page 445 Introduction to Rust and web programming......Page 447 Hyper as a client......Page 449 Hyper as a server......Page 458 Rocket......Page 461 Other web frameworks......Page 470 Exercises......Page 472 Summary......Page 474 Data Storage......Page 476 SQLite......Page 478 PostgreSQL......Page 481 Connection pooling with r2d2......Page 487 Diesel......Page 491 Summary......Page 497 Debugging......Page 499 Introduction to debugging......Page 501 GDB - basics......Page 504 GDB - threads......Page 509 LLDB - quick overview......Page 514 Editor integration with Visual Studio Code......Page 517 Exercises......Page 524 Summary......Page 526 Solutions and Final Words......Page 528 Chapter 1 - Getting Your Feet Wet......Page 530 Exercise - fix the word counter......Page 532 Chapter 2 - Using Cargo to Build Your First Program......Page 534 Exercise - starting our project......Page 536 Chapter 3 - Unit Testing and Benchmarking......Page 538 Exercise - fixing the tests......Page 540 Chapter 4 - Types......Page 543 Exercise - various throughout the chapter......Page 545 Chapter 5 - Error Handling......Page 550 Exercise solutions......Page 552 Chapter 6 - Memory, Lifetimes, and Borrowing......Page 556 Exercises......Page 558 Chapter 7 - Concurrency......Page 561 Exercises......Page 563 Chapter 8 - Macros......Page 568 Exercises......Page 570 Chapter 9 - Compiler Plugins......Page 573 Exercises......Page 575 Chapter 10 - Unsafety and Interfacing with Other Languages......Page 577 Exercises......Page 579 Chapter 11 - Parsing and Serialization......Page 582 Exercises......Page 584 Chapter 12 - Web Programming......Page 587 Exercises......Page 589 Chapter 13 - Data Storage......Page 592 Chapter 14 - Debugging......Page 594 Exercises......Page 596
Discover the powerful, hidden features of Rust you need to build robust, concurrent, and fast applicationsAbout This Book• Learn how concurrency works in Rust and why it is safe• Get to know the different philosophies of error handling and how to use them wisely• After reading this book, you will be able to migrate your legacy C or C++ application to a Rust environmentWho This Book Is ForThe target audience would be readers having knowledge of other programming languages and are able to work fluently in the operating system of their choice, be it Linux, OS X or Windows. Since Rust is a rather new language, they are interested in programming beyond simply using it for work. The book focuses on intermediate and advanced features of Rust.What You Will Learn• Implement unit testing patterns with the standard Rust tools• Get to know the different philosophies of error handling and how to use them wisely• Appreciate Rust's ability to solve memory allocation problems safely without garbage collection• Get to know how concurrency works in Rust and use concurrency primitives such as threads and message passing• Use syntax extensions and write your own• Create a Web application with Rocket• Use Diesel to build safe database abstractionsIn DetailIf concurrent programs are giving you sleepless nights, Rust is your go-to language. Being one of the first ever comprehensive books on Rust, it is filled with real-world examples and explanations, showing you how you can build scalable and reliable programs for your organization.We'll teach you intermediate to advanced level concepts that make Rust a great language. Improving performance, using generics, building macros, and working with threads are just some of the topics we'll cover. We'll talk about the official toolsets and ways to discover more. The book contains a mix of theory interspersed with hands-on tasks, so you acquire the skills as well as the knowledge. Since programming cannot be learned by just reading, we provide exercises (and solutions) to hammer the concepts in.After reading this book, you will be able to implement Rust for your enterprise project, deploy the software, and will know the best practices of coding in Rust.Style and approachThis book is your one stop guide to the Rust programming language and covers advanced-level concepts in a detailed manner using real-world examples.
Discover the powerful, hidden features of Rust you need to build robust, concurrent, and fast applications About This Book Learn how concurrency works in Rust and why it is safe Get to know the different philosophies of error handling and how to use them wisely After reading this book, you will be able to migrate your legacy C or C++ application to a Rust environment Who This Book Is For The target audience would be readers having knowledge of other programming languages and are able to work fluently in the operating system of their choice, be it Linux, OS X or Windows. Since Rust is a rather new language, they are interested in programming beyond simply using it for work. The book focuses on intermediate and advanced features of Rust. What You Will Learn Implement unit testing patterns with the standard Rust tools Get to know the different philosophies of error handling and how to use them wisely Appreciate Rust's ability to solve memory allocation problems safely without garbage collection Get to know how concurrency works in Rust and use concurrency primitives such as threads and message passing Use syntax extensions and write your own Create a Web application with Rocket Use Diesel to build safe database abstractions In Detail If concurrent programs are giving you sleepless nights, Rust is your go-to language. Being one of the first ever comprehensive books on Rust, it is filled with real-world examples and explanations, showing you how you can build scalable and reliable programs for your organization. We'll teach you intermediate to advanced level concepts that make Rust a great language. Improving performance, using generics, building macros, and working with threads are just some of the topics we'll cover. We'll talk about the official toolsets and ways to discover more. The book contains a mix of theory interspersed with hands-on tasks, so you acquire the skills as well as the knowledge. Since programming cannot be learned by just reading, we provide exercises (and solutions) to hammer the concepts in. After reading this book, you will be able to implement Rust for your enterprise project, deploy the software, and will know the best practices of coding in Rust. Style and approach This book is your one stop guide to the Rust programming language and covers advanced-level concepts in a detailed manner using real-world examples Key Features Book DescriptionIf concurrent programs are giving you sleepless nights, Rust is your go-to language. Filled with real-world examples and explanations, this book will show you how you can build scalable and reliable programs for your organization. We'll teach you big level concepts that make Rust a great language. Improving performance, using generics, building macros, and working with threads are just some of the topics we'll cover. We'll talk about the official toolsets and ways to discover more. The book contains a mix of theory interspersed with hands-on tasks so you acquire the skills as well as the knowledge. Since programming cannot be learned by just reading, we provide exercises (and solutions) to hammer the concepts in. After reading this book, you will be able to implement Rust for your enterprise project, deploy the software, and will know the best practices of coding in Rust. What you will learn