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
$9.99 $39.99
Paperback
$48.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
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

Shipping Address

Billing Address

Shipping Methods
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
Estimated delivery fee Deliver to United States

Economy delivery 10 - 13 business days

Free $6.95

Premium delivery 6 - 9 business days

$21.95
(Includes tracking information)

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 : 9781788839358
Category :
Languages :

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
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

Shipping Address

Billing Address

Shipping Methods
Estimated delivery fee Deliver to United States

Economy delivery 10 - 13 business days

Free $6.95

Premium delivery 6 - 9 business days

$21.95
(Includes tracking information)

Product Details

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

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 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
$199.99 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 $5 each
Feature tick icon Exclusive print discounts
$279.99 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 $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 146.97
Hands-On Concurrency with Rust
$48.99
Hands-On Functional Programming in Rust
$48.99
Rust Standard Library Cookbook
$48.99
Total $ 146.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

What is the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
What is custom duty/charge? Chevron down icon Chevron up icon

Customs duty are charges levied on goods when they cross international borders. It is a tax that is imposed on imported goods. These duties are charged by special authorities and bodies created by local governments and are meant to protect local industries, economies, and businesses.

Do I have to pay customs charges for the print book order? Chevron down icon Chevron up icon

The orders shipped to the countries that are listed under EU27 will not bear custom charges. They are paid by Packt as part of the order.

List of EU27 countries: www.gov.uk/eu-eea:

A custom duty or localized taxes may be applicable on the shipment and would be charged by the recipient country outside of the EU27 which should be paid by the customer and these duties are not included in the shipping charges been charged on the order.

How do I know my custom duty charges? Chevron down icon Chevron up icon

The amount of duty payable varies greatly depending on the imported goods, the country of origin and several other factors like the total invoice amount or dimensions like weight, and other such criteria applicable in your country.

For example:

  • If you live in Mexico, and the declared value of your ordered items is over $ 50, for you to receive a package, you will have to pay additional import tax of 19% which will be $ 9.50 to the courier service.
  • Whereas if you live in Turkey, and the declared value of your ordered items is over € 22, for you to receive a package, you will have to pay additional import tax of 18% which will be € 3.96 to the courier service.
How can I cancel my order? Chevron down icon Chevron up icon

Cancellation Policy for Published Printed Books:

You can cancel any order within 1 hour of placing the order. Simply contact customercare@packt.com with your order details or payment transaction id. If your order has already started the shipment process, we will do our best to stop it. However, if it is already on the way to you then when you receive it, you can contact us at customercare@packt.com using the returns and refund process.

Please understand that Packt Publishing cannot provide refunds or cancel any order except for the cases described in our Return Policy (i.e. Packt Publishing agrees to replace your printed book because it arrives damaged or material defect in book), Packt Publishing will not accept returns.

What is your returns and refunds policy? Chevron down icon Chevron up icon

Return Policy:

We want you to be happy with your purchase from Packtpub.com. We will not hassle you with returning print books to us. If the print book you receive from us is incorrect, damaged, doesn't work or is unacceptably late, please contact Customer Relations Team on customercare@packt.com with the order number and issue details as explained below:

  1. If you ordered (eBook, Video or Print Book) incorrectly or accidentally, please contact Customer Relations Team on customercare@packt.com within one hour of placing the order and we will replace/refund you the item cost.
  2. Sadly, if your eBook or Video file is faulty or a fault occurs during the eBook or Video being made available to you, i.e. during download then you should contact Customer Relations Team within 14 days of purchase on customercare@packt.com who will be able to resolve this issue for you.
  3. You will have a choice of replacement or refund of the problem items.(damaged, defective or incorrect)
  4. Once Customer Care Team confirms that you will be refunded, you should receive the refund within 10 to 12 working days.
  5. If you are only requesting a refund of one book from a multiple order, then we will refund you the appropriate single item.
  6. Where the items were shipped under a free shipping offer, there will be no shipping costs to refund.

On the off chance your printed book arrives damaged, with book material defect, contact our Customer Relation Team on customercare@packt.com within 14 days of receipt of the book with appropriate evidence of damage and we will work with you to secure a replacement copy, if necessary. Please note that each printed book you order from us is individually made by Packt's professional book-printing partner which is on a print-on-demand basis.

What tax is charged? Chevron down icon Chevron up icon

Currently, no tax is charged on the purchase of any print book (subject to change based on the laws and regulations). A localized VAT fee is charged only to our European and UK customers on eBooks, Video and subscriptions that they buy. GST is charged to Indian customers for eBooks and video purchases.

What payment methods can I use? Chevron down icon Chevron up icon

You can pay with the following card types:

  1. Visa Debit
  2. Visa Credit
  3. MasterCard
  4. PayPal
What is the delivery time and cost of print books? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela