ebook img

Rust in Action: Systems programming concepts and techniques PDF

456 Pages·2021·18.28 MB·english
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Rust in Action: Systems programming concepts and techniques

Systems programming concepts and techniques Timothy Samuel McNamara M A N N I N G Raw Pointer Box<T> Rc<T> Arc<T> The cousins*mutTand Store anything in a box.Accepts The reference counted pointer, Rc<T> Arc<T> is Rust’s ambassador. *constTare the free radicals almost any type for long-term is Rust's competent, yet miserly It can share values across threads, of the pointer world. Lightning storage.The workhorse of a bookkeeper. It knows who has guaranteeing that these will fast, but wildly unsafe. new, safe programming era. borrowed what and when. not interfere with each other. Powers Weaknesses Powers Weaknesses Powers Weaknesses Powers Weaknesses (cid:129)Speed (cid:129)Unsafe (cid:129)Store a value in (cid:129)Size increase (cid:129)Shared access (cid:129)Size increase (cid:129)Shared access (cid:129)Size increase (cid:129)Can interact with central storage to values (cid:129)Runtime cost to values (cid:129)Runtime cost the outside world icna alle ldoc“athtieo nheap” (cid:129)Not threadsafe (cid:129)Threadsafe Cell<T> RefCell<T> Cow<T> String An expert in metamorphosis, Performs mutation on immutable Why write something down when Acting as a guide on how to Cell<T> confers the ability to references with RefCel<T>. you only need to read it? Perhaps deal with the uncertainties of mutate immutable values. Its mind-bending powers you only want to make modifications. user input, String shows us how come with some costs. This is the role of Cow (copy on write). to build safe abstractions. Powers Weaknesses Powers Weaknesses Powers Weaknesses Powers Weaknesses (cid:129)Interior mutability (cid:129)Size increase (cid:129)Interior mutability (cid:129)Size increase (cid:129)Avoids writes (cid:129)Possible size (cid:129)Grows dynamically (cid:129)Can over (cid:129)Performance (cid:129)Can be nested (cid:129)Runtime cost when only read increase as required allocate size within Rc andArc, (cid:129)Lack of compile- access is used (cid:129)Guarantees correct which only accept time guarantees encoding at runtime immutable refs Arc<T> RawVec<T> Unique<T> Shared<T> Your program’s main storage system. The bedrock of Vec<T> and Sole owner of a value, Sharing ownership is hard. Vec<T> keeps your data orderly other dynamically sized types. a unique pointer is guaranteed Shared<T> makes life as values are created and destroyed. Understands how to provide a to possess full control. a little bit easier. home for your data as needed. Powers Weaknesses Powers Weaknesses Powers Weaknesses Powers Weaknesses (cid:129)Grows dynamically (cid:129)Can over (cid:129)Grows dynamically (cid:129)Not directly (cid:129)Base for types (cid:129)Not appropriate (cid:129)Shared ownership (cid:129)Not appropriate as required allocate size as required applicable from such as Strings, for application (cid:129)Can align memory for application (cid:129)Works with the your code requiring exclusive code directly toT’s width, even code directly memory allocator possession of values. when empty tofind space Rust in Action Rust in Action SYSTEMS PROGRAMMING CONCEPTS AND TECHNIQUES TIM MCNAMARA MANNING SHELTER ISLAND For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact Special Sales Department Manning Publications Co. 20 Baldwin Road PO Box 761 Shelter Island, NY 11964 Email: [email protected] ©2021 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine. Development editor: Elesha Hyde Technical development editor: René van den Berg Manning Publications Co. Review editor: Mihaela Batinic 20 Baldwin Road Production editor: Deirdre S. Hiam PO Box 761 Copy editor: Frances Buran Shelter Island, NY 11964 Proofreader: Melody Dolab Technical proofreader: Jerry Kuch Typesetter: Dennis Dalinnik Cover designer: Marija Tudor ISBN: 9781617294556 Printed in the United States of America To everyone aspiring to write safer software. contents preface xv acknowledgments xvii about this book xix about the author xxii about the cover illustration xxiii 1 Introducing Rust 1 1.1 Where is Rust used? 2 1.2 Advocating for Rust at work 3 1.3 A taste of the language 4 Cheating your way to “Hello, world!” 5 ■ Your first Rust program 7 1.4 Downloading the book’s source code 8 1.5 What does Rust look and feel like? 8 1.6 What is Rust? 11 Goal of Rust: Safety 12 ■ Goal of Rust: Productivity 16 Goal of Rust: Control 18 1.7 Rust’s big features 19 Performance 19 ■ Concurrency 20 ■ Memory efficiency 20 vii viii CONTENTS 1.8 Downsides of Rust 20 Cyclic data structures 20 ■ Compile times 20 ■ Strictness 21 Size of the language 21 ■ Hype 21 1.9 TLS security case studies 21 Heartbleed 21 ■ Goto fail; 22 1.10 Where does Rust fit best? 23 Command-line utilities 23 ■ Data processing 24 ■ Extending applications 24 ■ Resource-constrained environments 24 Server-side applications 25 ■ Desktop applications 25 Desktop 25 ■ Mobile 25 ■ Web 26 ■ Systems programming 26 1.11 Rust’s hidden feature: Its community 26 1.12 Rust phrase book 26 PART 1 RUST LANGUAGE DISTINCTIVES..........................29 2 Language foundations 31 2.1 Creating a running program 33 Compiling single files with rustc 33 ■ Compiling Rust projects with cargo 33 2.2 A glance at Rust’s syntax 34 Defining variables and calling functions 35 2.3 Numbers 36 Integers and decimal (floating-point) numbers 36 ■ Integers with base 2, base 8, and base 16 notation 37 ■ Comparing numbers 38 ■ Rational, complex numbers, and other numeric types 43 2.4 Flow control 45 For: The central pillar of iteration 45 ■ Continue: Skipping the rest of the current iteration 47 ■ While: Looping until a condition changes its state 47 ■ Loop: The basis for Rust’s looping constructs 48 ■ Break: Aborting a loop 48 ■ If, if else, and else: Conditional branching 49 ■ Match: Type-aware pattern matching 51 2.5 Defining functions 52 2.6 Using references 53 2.7 Project: Rendering the Mandelbrot set 54

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.