DEV Community

Cover image for Rust to Mainnet #002 — What Exactly Is Programming?
Dapper👾
Dapper👾

Posted on

Rust to Mainnet #002 — What Exactly Is Programming?

Understanding from the root.

Before we start writing Rust or even installing the tools we talked about in the previous article, I think it's important that we understand what programming actually is.

If you're already a developer, some of the things we'll discuss today might seem obvious to you. But remember, we're starting from zero, and I don't want to assume everyone following this series has written code before.

What Is Programming?

Imagine I asked you to go into the kitchen and make me a cup of tea.

You probably wouldn't need me to explain what a cup is, where to put the water, how to heat it or how much tea to add. You already have an understanding of how these things work.

Now imagine giving that same instruction to a computer.

The computer doesn't have the same understanding you do. Unless it has already been programmed with the necessary instructions and capabilities, telling it to make a cup of tea means nothing to it.

This is where programming comes in.

Programming is the process of creating instructions that a computer can execute to perform a particular task or solve a problem.

These instructions can involve performing calculations, storing information, making decisions, repeating actions or communicating with other computers.

Think about some of the applications you use every day.

When you send money through your banking application, someone has written instructions that determine how your balance is checked, how the transaction is processed and what happens when you don't have enough money.

When you send a message to someone, there are instructions responsible for processing that message and delivering it to the intended recipient.

Behind these applications are programs working together to perform different tasks.

What we're essentially learning to do is communicate with computers in a way that allows them to solve problems for us.

Algorithms and Problem Solving

Before writing a program, we need to understand the problem we're trying to solve and figure out how to solve it.

This brings us to a concept you'll encounter quite often in software development: algorithms.

An algorithm is a finite sequence of well-defined steps used to solve a problem or accomplish a task.

It doesn't necessarily have to involve a computer or a programming language.

Let's use an ATM withdrawal as an example.

Imagine we're responsible for determining how an ATM handles a withdrawal. A simplified process might look something like this:

  1. Ask the user to insert their card.

  2. Ask the user to enter their PIN.

  3. Verify that the PIN is correct.

  4. Ask how much money they want to withdraw.

  5. Check whether they have sufficient funds.

  6. If they have sufficient funds, process the withdrawal.

  7. Update their account balance and dispense the money.

  8. Return their card.

Seems straightforward, right?

But what happens if the PIN is incorrect?

What happens if the user enters a negative amount? What if the ATM doesn't have enough cash, even though the user has enough money in their account?

What happens if there's a network failure halfway through the transaction?

These are examples of situations we need to consider beyond the normal flow of our program. Some of them are what we call edge cases, while others are invalid inputs or operational failures.

As developers, we're not just responsible for making things work when everything goes according to plan. We also need to consider what happens when they don't.

The ATM example is obviously a simplified version of what happens in a real banking system, but it gives us an idea of how a problem can be broken down into smaller steps.

One thing I'd like you to take away from this section is that programming starts before you open your code editor.

Being able to write code is important, but knowing what instructions to write in the first place is just as important.

Inputs, Processing and Outputs

Most of the programs we'll be interacting with can be understood using three fundamental concepts:

  • Input: The information provided to a program.

  • Processing: What the program does with that information.

  • Output: The result produced by the program.

Let's take a calculator as an example.

If I enter 5 + 3 into a calculator, the numbers and the addition operator are my inputs. The calculation performed by the program is the processing, and the result, 8, is the output.

Now let's bring this back to the project we'll be building throughout this series: our Wallet API.

Imagine we have a wallet with a balance of ₦1,000, and we want to deposit another ₦500 into it.

Input

  • Current balance: ₦1,000

  • Deposit amount: ₦500

Processing

  • Check whether the deposit amount is valid.

  • Add the deposit amount to the current balance.

  • Update the wallet's balance.

Output

  • Deposit successful.

  • New balance: ₦1,500.

Notice that our program doesn't just produce an output. It also changes something internally: the wallet's balance.

The information a program maintains about its current condition is what we can refer to as its state.

In this example, our wallet's balance is part of its state. Before the deposit, the balance was ₦1,000. After the deposit, it's ₦1,500.

We'll explore state in more detail as we progress through Rust and eventually start building our backend applications and blockchain programs.

For now, just understand that a program can receive information, process it, produce a result and sometimes change its state along the way.

From Human Language to Machine Code

We've established that programming involves giving instructions to a computer. But how exactly do we give those instructions?

Why can't we just write a sentence in English and expect the computer to execute it?

To understand this, we need to understand a little about how computers work.

A computer's processor is designed to execute a specific set of machine instructions. These instructions tell it to perform operations such as adding numbers, moving data or comparing values.

At the machine level, these instructions are represented using binary encodings.

Binary is a system that uses two digits: 0 and 1.

Writing an entire application directly using machine code would be extremely difficult for most humans. Imagine trying to build our entire Wallet API by manually writing binary-encoded instructions.

This is one of the reasons programming languages exist.

They allow us to express instructions in a more readable and manageable form.

Here's a small example written in Rust:

fn main() {    println!("Hello, World!");}
Enter fullscreen mode Exit fullscreen mode

Don't worry about understanding the syntax just yet. We'll get to that in subsequent articles.

For now, look at how relatively readable that instruction is. Even without knowing Rust, you can probably guess that we're trying to display the words "Hello, World!" somewhere.

However, the processor doesn't directly execute Rust source code.

The code needs to be translated into instructions that the processor can execute. In Rust, this is primarily handled by a program called the Rust compiler, also known as rustc.

A simplified representation of what happens is:

Rust Source Code → Compiler and Linker → Executable → Processor

The actual compilation process involves several additional stages, which we'll explore later.

The important thing to understand for now is that programming languages give us a way to express instructions without having to manually write machine code.

Languages and Tools

If programming languages essentially allow us to communicate instructions to computers, why do we have so many of them?

Why do languages like Rust, Python, JavaScript, C and Java exist when they're all used to write programs?

Different programming languages were designed with different priorities and purposes in mind.

Some prioritize ease of development, some prioritize performance, while others focus on things like memory safety, portability or making certain types of applications easier to build.

For example:

  • Python is commonly used for automation, data science, artificial intelligence and backend development.

  • JavaScript is widely used in web development, both in browsers and on servers.

  • C is commonly used in operating systems, embedded systems and low-level programming.

  • Rust is used in systems programming, backend engineering, blockchain development and applications where performance and memory safety are important.

This doesn't mean these languages are restricted to those areas. There are often multiple languages you can use to solve the same problem.

We'll discuss why I chose Rust for this series in a subsequent article.

For now, let's familiarize ourselves with a few terms you'll be seeing frequently.

Source Code

Source code is the code we write using a programming language.

When we eventually create a Rust file with the .rs extension and start writing instructions inside it, what we're writing is Rust source code.

Compiler

A compiler is a program that translates code from one language or representation into another.

In the case of Rust, the compiler and its associated tools typically transform our source code into an executable containing machine code.

The compiler also performs various checks on our code and can detect certain errors before we run the program.

Interpreter

An interpreter is a program that executes instructions from another program, either directly from source code or from an intermediate representation.

For example, CPython, the most widely used implementation of Python, compiles Python source code into bytecode and executes that bytecode using a virtual machine.

Unlike the typical Rust development process, we generally don't need to produce a standalone native executable before running a Python script.

It's worth noting that compilation and interpretation aren't always mutually exclusive. Modern language implementations can combine both approaches.

Executable

An executable is a file containing a program that an operating system can load and run.

When we compile a normal Rust application, we typically produce an executable that can be run without needing to compile the source code again.

Code Editor

A code editor is simply a tool that helps us write and manage our source code.

Visual Studio Code, which we'll be using throughout this series, provides features like syntax highlighting, code completion and integration with development tools.

The editor itself is not the Rust compiler. It helps us write our code and interact with the tools responsible for compiling and running it.

We'll see how all these tools work together when we set up our development environment.

Your First Exercise

We're not going to write any Rust code today.

Instead, I want you to think like a developer for a moment.

Imagine you're building a simple wallet application that allows users to deposit money, withdraw money and check their balance.

The wallet starts with a balance of ₦1,000.

Write down the steps your program would need to follow to handle the following operations:

  • Deposit ₦500.

  • Withdraw ₦300 after the deposit.

  • Display the remaining balance.

Once you've done that, consider the following situations:

  • What happens if someone tries to deposit a negative amount?

  • What happens if someone enters zero as the deposit amount?

  • What happens if someone tries to withdraw more money than they have?

  • What happens if someone enters text instead of a number?

  • Should the balance change when an invalid transaction occurs?

  • What information should the program display to the user when something goes wrong?

Try to express your solution as a sequence of instructions that leaves as little room for ambiguity as possible.

You can do this on a piece of paper or in your notes application. There's no need to know any programming syntax yet.

We'll eventually turn these instructions into actual Rust code.

Before We Move On

I hope by now you have a clearer understanding of what programming is and why it involves more than just writing code.

Programming requires us to understand problems, break them down into smaller steps and express those steps in a way that a computer can execute.

In the next article, we'll take a closer look at the machine we're giving these instructions to.

How do computers actually work?

We'll be exploring processors, memory, storage and what happens when a computer executes a program.

See you in the next one. 🦀

Additional Learning Resources

If you'd like to explore some of the concepts discussed in this article further, here are a few useful resources:


Part of Rust to Mainnet, a free, open-source learning series.

Complete Curriculum | GitHub Repository

Top comments (0)