Menu Close

Articles

Rust: Overview

Rust is a system programming language. It was designed to replace C for critical applications that require performance and safety. Next article will explain basic syntax elements that may be familiar to you if you have study C or any other structured programming language.

Syntax Rules

Rust syntax is curly braced like C++ and Java. This means every block of code is enclosed in curly braces “{ … }”. In my opinion this is not an advantage since the curly braces are best used for a data set or a dictionary collections.

Second thing about syntax is the end of statement is terminated by “;” like in C. Rust use abbreviations not full English keywords.  This again is a disadvantage since you have to learn the abbreviation and its meaning for writing Rust code.

Example:

In this example fn = function. println!()  is a “macro” not a method or function. “!” is used to signal that is a macro you can call like a function.

Every Rust program has at least one function: the main() function. This is the the program entry point. If the main() function is missing, it can not be run. This is very common approach in structured programming languages, but not all programming language have a main() function.

Homework: open this program and run it live: hello world

Comments

Single line Comments start with two slashes //. Block comments is enclosed using two symbols: /*… */ exactly like in C++. We know this cause issues with nested comments that I think are not resolved in Rust. There are other conventions for example “/** … */ ” is recognized as documentation in Rust.

Variables

The variable declaration in Rust is done using keyword “let” and symbol “=”. Once a variable is initialized we call this “binding”. So a variable is bound to it’s value. A variable that is not bound can’t be used. To assign a type to a variable we use symbol “:” after the variable name.

A variable is an identifier or a name that can start with “_” or any alphabetic character but not a special character or a number. So this is almost like any other language.To make a variable mutable we declare it using additional keyword “mut”. If you forget this, your variable is actually constant.

Examples:

Assignment

The assignment operator “=” works only with variables that are mutable and bound to initial value. In this case we can change the value of the variable. So you see there is a difference between “let” that initialize a variable and “=” that assign a value to a variable.

Variable Scope

In English the “scope” has two meanings:

  • “the extent of the area or subject matter that something deals with or to which it is relevant”
  • “the opportunity or possibility to do or deal with something.”

This may confuse some for the meaning of “Variable Scope”. We use the first meaning: The “scope” of a variable is the area or the block of code where the variable is visible and relevant.

code block

We have this concept of code block that is a compact region of code separated by squiggly brackets: {….}. In other languages we start a block using “begin” and terminate with “end” keywords. Having brackets used as block of code makes Rust a little bit harder to understand, but we know how to use spaces and new lines to organize our code.

Indentation

In Rust the indentation is not relevant but recommended. What is relevant is that we can have nested blocks of code. The outer block can contain one or more inner blocks. Each block of code define a local scope.

Shadowing

All variables defined in outer scope are visible in the inner scope. We can hide a variable by rebinding the name to a new value using “let”. In this case the external variable will be hidden or “shadowed”. Variable shadowing can change the type and the value of the variable name.

Examples:

Notes: 

  • x is immutable, but if you declare x a second time inside the bloc, it is a new variable. So it look like you have accomplish something. You have manage to change the value and the type of a variable.
  • this is not a trick. You have not done anything to variable x except you have hide it, so another variable has now the same name but a different type.

Homework: open this example and run it on-line: shadowing

Function

The function is a common way to create reusable code and split a larger problem into smaller parts. A function is a named block of code that can receive parameters and can return a result.

Example:

Notes:

  1. In Rust we define a function using “fn” keyword then name of the function. The result type can be declared using symbol “->”. In other languages we may use “=>”.
  2. In the function body we can use return statement to create the function result and stop function execution in the same time.
  3. Result of a function, symbol “->” after function parameters and the return statement are both optional.

Homework: Open this example live and run it: functions

Delimiters

These are ASCII characters or group of two characters used to separate symbols, identifiers or literals.

SymbolDescription
:define type
;statement separator, define array element type
,enumeration
.dot operator or decimal separator
\string continuation or escape code
->define result type
>relation operator: greater then
<relation operator: less then
//single line comment (can be used at start of line or end of line after “;”
/*…*/block comment. Other variations: /** …. */ or /*! …. */ each with slightly different purpose.
&borrow
*raw pointer
..range of numbers or update operator for struct literals
->define result type of a function
@pattern bindings
_ignored
?error propagation symbol
=>used in match patterns
inclusive range pattern
[…]array literal, vector literal
{…}block of code, struct literal, interpolation placeholder
(…)tuple, expression, parameters
<…>declare generic data types
#meta (compiler directive)
$macro substitution
::file based scoping operator similar to “.”
m!macro notation for example: print!(). It is also used as logical operator. Do not confuse the two.
‘…’character literal
“…”delimiters for string literal

Keywords

In Rust when you define a new item you must avoid using the reserved keywords. These are used only to describe language elements and not as names for variables, functions or types.  You do not have to memorize them just rad them once to understand better what you will need to learn in the future.

as box break const continue crate else enum extern false fn for if impl in let loop match mod move mut pub ref return self Self static struct super trait true type unsafe use where while  
Read next: Data Types