Programming languages are typically taught by following an index of the programming constructs of the language. Declaring and using variables is followed by writing arithmetic statements is followed by writing if statements is followed by writing loops and so on.
But computer programming is primarily a problem-solving activity and one way it can be taught is by focusing on the types of problems you can solve with the language.
In this series of articles I am going to teach the basics of Rust programming by organizing the lessons around a set of program templates. These templates were first introduced to me in a book titled Designing Pascal Solutions, written by Michael J. Clancy and Marcia C. Linn. In this introductory article I’m going to define program templates and demonstrate just a couple of them to show how they can be used to teach Rust programming more effectively.
To quote Clancy and Linn, a program template is “a reusable abstraction of programming knowledge constructed by students.” Programmers today may know them better by the term “design pattern.”
A program template is defined by its description and purpose and then some pseudocode to illustrate how the template is used. Let’s look at an example. The first template I’ll introduce is Prompt, Then Read. Here is the description and purpose of this template:
This template is used to request a value from the user and to read it into a variable. The template is implemented by writing out an informative message, a prompt, to the user telling them what to input and then calling a procedure (function) to receive the input.
Here is an example of the pseudocode for Prompt, Then Read:
Print a message asking for a value
Read the value
Here is an example:
println!("Enter a number: ");
. expect("Could not read line.");
The Input, Process, Output template is used to read some values into the program, process those values, and display the results.