Programming Rust

Programming Rust

Language: English

Pages: 400

ISBN: 1491927283

Format: PDF / Kindle (mobi) / ePub


This practical book introduces systems programmers to Rust, the new and cutting-edge language. You’ll learn how Rust offers the rare and valuable combination of statically verified memory safety and low-level control—imagine C++, but without dangling pointers, null pointer dereferences, leaks, or buffer overruns.

Author Jim Blandy—a former maintainer of GNU Emacs and GNU Guile—demonstrates how Rust has the potential to be the first usable programming language that brings the benefits of an expressive modern type system to systems programming. Rust’s rules for borrowing, mutability, ownership, and moves versus copies will be unfamiliar to most systems programmers, but they’re key to Rust’s unique advantages.

This book presents Rust’s rules clearly and economically; elaborates on their consequences; and shows you how to express the programs you want to write in terms that Rust can prove are free of broad classes of everyday errors.

Learning AngularJS: A Guide to AngularJS Development

Secure Programming Cookbook for C and C++: Recipes for Cryptography, Authentication, Input Validation & More

Pattern-Oriented Software Architecture: A System of Patterns (Pattern-Oriented Software Architecture, Volume 1)

The Little Book on CoffeeScript

Processing: A Programming Handbook for Visual Designers and Artists

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

std::i32, std::u8, and so on. assert_eq!(2u16.pow(4), 16); // exponentiation assert_eq!((-4i32).abs(), 4); // absolute value assert_eq!(0b101101u8.count_ones(), 4); // population count The type suffixes on the literals are required here: Rust can’t look up a value’s meth‐ ods until it knows its type. In real code, however, there’s usually additional context to disambiguate the type, so the suffixes aren’t needed. 46 | Chapter 3: Basic types www.it-ebooks.info Floating-point types Rust

starts to look like a surprisingly good deal when we consider its consequences for multithreaded programming. Con‐ currency is notoriously difficult to use correctly in C and C++; developers usually turn to concurrency only when single-threaded code has proven unable to achieve the performance they need. But Rust’s particular form of type safety guarantees that concurrent code is free of data races, catching any misuse of mutexes or other syn‐ chronization primitives at compile time, and

create mutable portions of otherwise immutable values; if you com‐ bine those techniques with Rc pointers, you can create a cycle and leak memory. Ownership and moves www.it-ebooks.info | 85 1. In the past, some C++ libraries shared a single buffer among several std::string values, using a reference count to decide when the buffer should be freed. Newer versions of the C++ specification effectively preclude that representation; all modern C++ libraries use the approach shown here.↩

Rust has fended off a wide class of everyday mistakes. The immiscibility of shared and mutable references also really shines when writing concurrent code. A data race is only possible when some value is both shared shared between threads and mutable—which is exactly what Rust’s reference rules eliminate. A concurrent Rust program that avoids unsafe code is free of data races by construc‐ tion. We’ll cover concurrency in detail in “Concurrency” on page 28. Rust’s shared references versus C’s

starts with ::. ::std refers to the top-level module of the standard library (regardless of anything else you might have declared locally with the name std). ::std::mem is a submodule within the standard library, and ::std::mem::swap is a public function in that module. You could write all your code this way, spelling out ::std::f64::consts::PI and ::std::collections::HashMap::new every time you want a circle or a dictio‐ nary. The alternative is to import features into the modules where they are

Download sample

Download

About admin