Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Hands-On Functional Programming in Rust
Hands-On Functional Programming in Rust

Hands-On Functional Programming in Rust: Build modular and reactive applications with functional programming techniques in Rust 2018

eBook
₹799 ₹2919.99
Paperback
₹3649.99
Subscription
Free Trial
Renews at ₹800p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Hands-On Functional Programming in Rust

Functional Control Flow

The control flow is the most basic building block of programming. Early languages had no concept of data structures or functions, only program flow. These control flow structures have evolved over time, from simple branches and loops to the complex value expressions available in Rust.

In this chapter, we will start developing the project that will form the basis of all code examples in this book. The first project's requirements are introduced immediately. Then, we will provide you with actionable steps to transform project requirements into a code outline with tests. Lastly, we will develop code for the full deliverable.

Learning outcomes:

  • Gathering project requirements
  • Architecting a solution based on project requirements
  • Using and recognizing expressions in functional style
  • Testing the solution with integration and unit tests
...

Technical requirements

Designing the program

To design the program, let's look at the various aspects required for the project.

Gathering project requirements

Consider this situation: Your engineering firm is being considered for a contract to design software to control the elevators for a real estate developer. The contract lists three buildings under development with various heights and non-uniform elevator designs. The elevator designs are being finalized by other subcontractors and will become available shortly after the software contract is awarded.

To submit your proposal, your firm should demonstrate the basic capabilities of your elevator control software. Then, once awarded, you will be expected to integrate these capabilities into...

Implementing program logic

Once a stub program has been created, we can proceed to replace stubs with working code.

Filling in the blanks

Now that we have code stubs and a map of each feature that needs to be implemented, we can begin writing the code logic. At this point, if you are working on a team, then this would be a good time to divide the work. The architecture phase may be done by one person, or as a team, but it can't be done in parallel. In contrast, the implementation phase can be broken into parts to work on separately.

Parsing input and storing as building description and floor requests

...

Writing tests

To run tests from the command line, type cargo test. We will be doing this a lot.

Unit testing

Unit testing focuses on testing internal interfaces and components of a program. It is also called whitebox testing. To first create unit tests, it is a good idea to look at all of the top-level types, traits, and functions. All top-level identifiers make for good test cases. Depending on the structure of the program, it may also be a good idea to test combinations of these components to cover expected use cases.

We have one utility function, the statistic calculation, which would be a good candidate to write a unit test for. However, this function doesn't return any result. Instead, it immediately prints output...

Summary

In this chapter, we outlined the steps taken to gather project requirements, architect a solution, and then implement the completed deliverable. We focused on how this process can be clarified using functional thinking.

When gathering program requirements, the required data, input, and output should be clarified. When translating requirements into a code plan, creating a dependency graph as an intermediary step can help simplify complex designs. When testing, functions become great units to cover. By comparison, lines and lines of imperative code are almost impossible to test.

We will continue to develop this software project throughout the book. This first simulation deliverable will accompany the project proposal and will hopefully help our firm be selected for the contract. In the next chapter, you will receive feedback from the developers and meet your competitor.

...

Questions

  1. What is the ternary operator?
  2. What is another name for unit tests?
  3. What is another name for integration tests?
  4. What is declarative programming?
  5. What is imperative programming?
  6. What is defined in the iterator trait?
  7. In which direction will fold traverse the iterator sequence?
  8. What is a dependency graph?
  9. What are the two constructors of Option?
Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • •Learn generics, organization, and design patterns in functional programming
  • •Modularize your applications and make them highly reusable and testable using functional design patterns
  • •Get familiar with complex concepts such as metaprogramming, concurrency, and immutability

Description

Functional programming allows developers to divide programs into smaller, reusable components that ease the creation, testing, and maintenance of software as a whole. Combined with the power of Rust, you can develop robust and scalable applications that fulfill modern day software requirements. This book will help you discover all the Rust features that can be used to build software in a functional way. We begin with a brief comparison of the functional and object-oriented approach to different problems and patterns. We then quickly look at the patterns of control flow, data the abstractions of these unique to functional programming. The next part covers how to create functional apps in Rust; mutability and ownership, which are exclusive to Rust, are also discussed. Pure functions are examined next and you'll master closures, their various types, and currying. We also look at implementing concurrency through functional design principles and metaprogramming using macros. Finally, we look at best practices for debugging and optimization. By the end of the book, you will be familiar with the functional approach of programming and will be able to use these techniques on a daily basis.

Who is this book for?

This book is for Rust developers who are comfortable with the language and now want to improve their coding abilities by learning advanced functional techniques to enhance their skillset and create robust and testable apps.

What you will learn

  • •How Rust supports the use of basic functional programming principles
  • •Use functional programming to handle concurrency with elegance
  • •Read and interpret complex type signatures for types and functions
  • •Implement powerful abstractions using meta programming in Rust
  • •Create quality code formulaically using Rust s functional design patterns
  • •Master Rust s complex ownership mechanisms particularly for mutability

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : May 31, 2018
Length: 249 pages
Edition : 1st
Language : English
ISBN-13 : 9781788831581
Category :
Languages :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : May 31, 2018
Length: 249 pages
Edition : 1st
Language : English
ISBN-13 : 9781788831581
Category :
Languages :

Packt Subscriptions

See our plans and pricing
Modal Close icon
₹800 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
₹4500 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just ₹400 each
Feature tick icon Exclusive print discounts
₹5000 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just ₹400 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 10,949.97
Hands-On Concurrency with Rust
₹3649.99
Hands-On Functional Programming in Rust
₹3649.99
Rust Standard Library Cookbook
₹3649.99
Total 10,949.97 Stars icon
Banner background image

Table of Contents

11 Chapters
Functional Programming – a Comparison Chevron down icon Chevron up icon
Functional Control Flow Chevron down icon Chevron up icon
Functional Data Structures Chevron down icon Chevron up icon
Generics and Polymorphism Chevron down icon Chevron up icon
Code Organization and Application Architecture Chevron down icon Chevron up icon
Mutability, Ownership, and Pure Functions Chevron down icon Chevron up icon
Design Patterns Chevron down icon Chevron up icon
Implementing Concurrency Chevron down icon Chevron up icon
Performance, Debugging, and Metaprogramming Chevron down icon Chevron up icon
Assessments Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Half star icon Empty star icon Empty star icon 2.8
(4 Ratings)
5 star 25%
4 star 0%
3 star 25%
2 star 25%
1 star 25%
Amazon Customer May 10, 2020
Full star icon Full star icon Full star icon Full star icon Full star icon 5
There is a certain style you can almost expect with language books with titles this broad, but this particular book was written on an intermediate level to focus of introducing FP concepts to an existing Rust knowledge, where all of the general concepts of Rust are assumed common knowledge (See page 3: to get the most out of this book). Instead of the book being written around some larger project (a common practice when teaching general language concepts), this book has instead been written as a collection of common practices and patterns which apply to the FP paradigm within Rust. While you cannot expect to read the code examples and write them as working projects, the book does offer a repository of working examples for each concept discussed, so you can still pick it apart and experiment, or simply see how it's used.As someone who was not a stranger to FP programming in other languages, nor writing simple Rust apps, I found this book to be absolutely ideal for my needs. I was only ever interested in how apply FP idioms and patterns, while seeing how constructs behaved in different scenarios of meta-programming. Not having to deal with the bloat that would come from teaching general concepts, made for a much shorter read. I'm not sure if this niche was intended, but If you share the same use-case, I highly recommend this book.
Amazon Verified review Amazon
John Chandler Oct 05, 2020
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3
Coverage of Rust's functional programming capabilities is sadly quite light, and often only mentioned in passing, which is a shame because it's quite a powerful and expressive part of the language.This book seemed like the answer I'd been looking for - a book specifically on functional programming in Rust. However, all is not what it seems. Yes, there is some functional programming scattered around the book and it does go into things a bit more than other Rust books, but only just. It mostly seems to cover designing and refactoring a lift/elevator software codebase, with pages of code excerpts that don't really illustrate functional programming. For example, a chapter on mutability and ownership touches on the FFI (Foreign Function Interface) and unsafe functions. Interesting topics, but probably not the ideal way to approach the intended subject especially when presenting the reader with many pages of code.I've rated the book three stars based on the promise of the title. If you're new to functional programming, new to Rust, or trying to find out more about Rust's functional side then you'll probably be disappointed. There's some useful functional stuff buried in the book, but you'll have to dig around a bit.However, if you're new to writing larger projects, not necessarily in Rust, you'll probably find this book of more interest. There's content on structuring your project, some more advanced aspects of Rust, a handful of design patterns, and bits on concurrency, performance, debugging, and defensive programming. In some sense, the book should've been marketed more towards these than the functional programming side of things - I can see a lot of readers who could benefit from this book overlooking it because of the title.
Amazon Verified review Amazon
Steve Loveless Jun 06, 2019
Full star icon Full star icon Empty star icon Empty star icon Empty star icon 2
I've started this book twice now and just can't get passed 30% or so (on my Kindle). I've been doing Rust for about three years now and have been wanting to get into functional programming; so I got this book. The first bit of the book makes it sound hopeful, but then tries to dig in to content using chapters worth of working through some example "project". ...which could be fine, but much of that is spent talking about project management ideas (which in 20 years of software development are not familiar ones to me at that) and the science behind that project (pages and pages of controller software for an elevator in a building, so how to calculate acceleration, velocity, etc). If there have been FP concepts introduced, they've been lost in all of this unrelated content, leaving me frustrated with this book.
Amazon Verified review Amazon
Sean Bergstedt Mar 17, 2021
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1
I was really hoping this book would shine some light on best practices with employing functional programming methods in Rust, but I ended up spending most of my time trying to wrap my head around dense code and terse explainations. I had to give up half way through as I just couldn't justify the remaining time investment when the first half brought me no value.I've bought several Packt publishing books and while I do appreciate their willingness to push out titles of bleeding edge topics ahead of other publications, it often comes as the cost of quality.I believe the author made a valiant effort to pull together some material relating to functional programming in Rust. However, the example they use is so dense you spend chapters just trying to decipher what's going on with the code vs actually seeing how to employ a functional approach to development. Few alternatives are offered as contrast either, say, by showing what one might do in an OO style, then showing how it would be different functionally. Given that Rust is very flexibly do both, it's really important to see the contrast and get a snapshot of how they relate..The book is just does not deliver on its title. Wait for something better.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.