Vaultree
Published in

Vaultree

Functions and Control Flow in Rust

Continue your Rust journey by Exploring the basics of functions and control flow.

First, let’s recap. Rust is a programming language developed by Mozilla Corporation. It is famous for its safety, speed, and concurrency, making it a popular choice for building high-performance systems, and that’s why we are learning more about it.

Control Flow

Control flow refers to the order in which statements, expressions, and other language constructs are executed in a program. In Rust’s programming language, control flow is implemented using a combination of keywords, expressions, and statements.

One of the fundamental control flow constructs in Rust is the if statement. That allows a programmer to specify the condition for executing a block of code. For example:

Another important control flow construct in Rust is the match expression. It allows a programmer to compare a value against multiple patterns and execute different codes for each match. For example:

Rust also provides the loop keyword, allowing the creation of infinite loops. These can be useful for repeating a certain block of code until a certain condition is met. For example:

In addition to these control flow constructs, Rust provides several other keywords and expressions to control the flow of a program, including while, for, and continue.

In Rust, the while keyword is used to create a while loop. It repeatedly executes a block of code until a specified condition is met. The syntax for a while loop is:

The for keyword is used to create a for loop. It repeatedly executes a block of code for a given number of times. The syntax for a for loop is:

The continue keyword is used within a loop to immediately skip the rest of the current iteration of the loop and start the next iteration. This can be used to skip over certain iterations of the loop based on certain conditions. The syntax for using continue is:

In this example, the continue keyword is used within a while loop to skip over the current iteration of the loop if some_condition is true. The code inside the loop will only be executed if some_condition is false.

Together, these features provide Rust programmers with a powerful set of tools for implementing the control flow of their programs.

Functions

Functions are a fundamental building block of any programming language, and Rust is no exception. In Rust, functions are created using the fn keyword, followed by the name of the function and a set of parentheses containing the function’s parameters. For example:

Functions in Rust can also return a value using the return keyword. In the example above, the -> i32 syntax after the parameter list indicates that the function will return an i32 (32-bit signed integer) value.

Control flow is an important aspect of functions in Rust, as it allows a programmer to specify the different paths a function can take depending on the input it receives. For example, the add function above could use an if statement to check whether the two numbers being added are both positive, and return a different value if that is not the case:

Another way control flow can be used in functions is by using match expressions. They allow a function to handle multiple cases and return different values depending on the input. For example, the following function uses a match expression to determine whether a given number is positive, negative, or zero, and returns a string indicating the result:

The code example above defines a function named classify that takes an i32 (32-bit signed integer) value as its parameter and returns a String value. The function uses a match expression to compare the value of the parameter x against three different patterns:

  1. The first pattern, 0, matches the value of x if it equals to zero. In this case, the code “zero”.to_string() is executed, which creates a new string containing the text “zero” and returns it as the result of the function.
  2. The second pattern, n if n > 0, matches the value of x if it is greater than zero. This pattern also binds the value of x to a variable named n, which can be used in the code that is executed if this pattern matches. In this case, the code “positive”.to_string() is executed, which creates a new string containing the text “positive” and returns it as the result of the function.
  3. The third pattern, n if n < 0, matches the value of x if it is less than zero. This pattern also binds the value of x to the variable n, which can be used in the code that is executed if this pattern matches. In this case, the code “negative”.to_string() is executed, which creates a new string containing the text “negative” and returns it as the result of the function.

If the value of x does not match any of these patterns, the match expression will return an error. This happens because the match expression is exhaustive, that is, it must cover all possible cases. In this example, however, it is not possible for x to take on any value other than the three cases covered by the patterns, so the function is correct as written.

This code is a great example of how control flow can be used in a Rust function to handle cases and return values depending on the input.

Now that we have published this series of articles on Rust fundamentals, you should understand the basics of this programming language. Now you can start building simple applications in Rust, using the features and tools the language provides.

Hands-on

Wrapping up what we’ve learnt in the last three articles and let’s build a function that checks if a number is part of the Fibonacci sequence in Rust:

This function uses a simple loop to generate the Fibonacci sequence. It starts with the first two numbers in the sequence, 0 and 1, and then uses a while loop to generate the next numbers in the sequence by adding the previous two numbers together. The loop continues until the generated number is greater than or equal to n. If the generated number is equal to n, the function returns true because n is part of the Fibonacci sequence. If the generated number is not equal to n, the function returns false because n is not part of the Fibonacci sequence.

Conclusion

In conclusion, Rust is a powerful and efficient programming language with a rich set of features and tools for building a wide range of applications. With the information provided in these articles, our community should be well-equipped to experiment with Rust and learn more about its capabilities.

Leave a comment and let us know what you want to learn next. Is there a particular topic or aspect of Rust that you would like us to cover in future articles? Do you have any questions or concerns about Rust you would like us to address? We built this space for you, so we’d love to hear your feedback and suggestions.

About Vaultree

Vaultree has developed the world’s first Fully Functional Data-in-Use Encryption solution that solves the industry’s fundamental security issue: persistent data encryption, even in the event of a leak. Vaultree enables enterprises, including those in the financial services and healthcare / pharmaceutical sectors, to mitigate the great financial, cyber, legal, and business risk of a data breach in plain text. With Vaultree, organisations process, search, and compute ubiquitous data at scale, without ever having to surrender encryption keys or decrypt server-side. If a leak occurs, Vaultree’s data-in-use encryption persists, rendering the data unusable to bad actors. Integrating Vaultree into existing database technologies is seamless, requiring no technology or platform changes. Vaultree is a privately held company based in Ireland and the U.S.

For more information, please visit www.vaultree.com.

--

--

Vaultree’s Encryption-in-use enables businesses of all sizes to process (search and compute) fully end-to-end encrypted data without the need to decrypt.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Vaultree Tech Community

Vaultree is a revolutionary encryption startup allowing processing of fully encrypted data at run-time. Let’s create an encrypted tomorrow…together!