The Rust Programming Language by Steve Klabnik & Carol Nichols
Author:Steve Klabnik & Carol Nichols [Klabnik, Steve]
Language: eng
Format: epub, mobi
ISBN: 9781593278519
Publisher: No Starch Press
Published: 2018-07-09T23:00:00+00:00
The output will be the recommended workout plan. Listing 13-2 shows the main function we’ll use.
src/main.rs
fn main() {
let simulated_user_specified_value = 10;
let simulated_random_number = 7;
generate_workout(
simulated_user_specified_value,
simulated_random_number
);
}
Listing 13-2: A main function with hardcoded values to simulate user input and random number generation
We’ve hardcoded the variable simulated_user_specified_value as 10 and the variable simulated_random_number as 7 for simplicity’s sake; in an actual program, we’d get the intensity number from the app’s frontend, and we’d use the rand crate to generate a random number, as we did in the Guessing Game example in Chapter 2. The main function calls a generate_workout function with the simulated input values.
Now that we have the context, let’s get to the algorithm. The function generate_workout in Listing 13-3 contains the business logic of the app that we’re most concerned with in this example. The rest of the code changes in this example will be made to this function.
src/main.rs
fn generate_workout(intensity: u32, random_number: u32) {
➊ if intensity < 25 {
println!(
"Today, do {} pushups!",
simulated_expensive_calculation(intensity)
);
println!(
"Next, do {} situps!",
simulated_expensive_calculation(intensity)
);
} else {
➋ if random_number == 3 {
println!("Take a break today! Remember to stay hydrated!");
➌ } else {
println!(
"Today, run for {} minutes!",
simulated_expensive_calculation(intensity)
);
}
}
}
Listing 13-3: The business logic that prints the workout plans based on the inputs and calls to the simulated_expensive_calculation function
The code in Listing 13-3 has multiple calls to the slow calculation function. The first if block ➊ calls simulated_expensive_calculation twice, the if inside the outer else ➋ doesn’t call it at all, and the code inside the second else case ➌ calls it once.
The desired behavior of the generate_workout function is to first check whether the user wants a low-intensity workout (indicated by a number less than 25) or a high-intensity workout (a number of 25 or greater).
Low-intensity workout plans will recommend a number of push-ups and sit-ups based on the complex algorithm we’re simulating.
If the user wants a high-intensity workout, there’s some additional logic: if the value of the random number generated by the app happens to be 3, the app will recommend a break and hydration. If not, the user will get a number of minutes of running based on the complex algorithm.
This code works the way the business wants it to now, but let’s say the data science team decides that we need to make some changes to the way we call the simulated_expensive_calculation function in the future. To simplify the update when those changes happen, we want to refactor this code so it calls the simulated_expensive_calculation function only once. We also want to cut the place where we’re currently unnecessarily calling the function twice without adding any other calls to that function in the process. That is, we don’t want to call it if the result isn’t needed, and we still want to call it only once.
Download
The Rust Programming Language by Steve Klabnik & Carol Nichols.mobi
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Deep Learning with Python by François Chollet(12569)
Hello! Python by Anthony Briggs(9914)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9795)
The Mikado Method by Ola Ellnestam Daniel Brolund(9777)
Dependency Injection in .NET by Mark Seemann(9337)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8296)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7763)
Grails in Action by Glen Smith Peter Ledbrook(7696)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7557)
Becoming a Dynamics 365 Finance and Supply Chain Solution Architect by Brent Dawson(7062)
Microservices with Go by Alexander Shuiskov(6824)
Practical Design Patterns for Java Developers by Miroslav Wengner(6741)
Test Automation Engineering Handbook by Manikandan Sambamurthy(6684)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6413)
Angular Projects - Third Edition by Aristeidis Bampakos(6088)
The Art of Crafting User Stories by The Art of Crafting User Stories(5615)
NetSuite for Consultants - Second Edition by Peter Ries(5554)
Demystifying Cryptography with OpenSSL 3.0 by Alexei Khlebnikov(5356)
Kotlin in Action by Dmitry Jemerov(5062)
