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

Arrow left icon
Profile Icon Andrew Johnson
Arrow right icon
$19.99 per month
Full star icon Full star icon Half star icon Empty star icon Empty star icon 2.8 (4 Ratings)
Paperback May 2018 249 pages 1st Edition
eBook
$9.99 $39.99
Paperback
$48.99
Subscription
Free Trial
Renews at $19.99p/m
Arrow left icon
Profile Icon Andrew Johnson
Arrow right icon
$19.99 per month
Full star icon Full star icon Half star icon Empty star icon Empty star icon 2.8 (4 Ratings)
Paperback May 2018 249 pages 1st Edition
eBook
$9.99 $39.99
Paperback
$48.99
Subscription
Free Trial
Renews at $19.99p/m
eBook
$9.99 $39.99
Paperback
$48.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
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 : 9781788839358
Category :
Languages :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

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 included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.