Mozilla's systems programming language Rust
Pit Stop

© Lead Image © Paul Maydikov, 123RF.com
We look at a few features of Rust, Mozilla's systems programming language, and its similarity to other languages.
The statically typed programming language Rust [1] places much emphasis on speed and security. Rust, like its inspiration C, stores fixed-sized variables in a stack. However, it stores pointer values in the main memory heap. The code in Listing 1 demonstrates controlled handling of allocated memory in Rust. If you compile Listing 1 in the shell via
rustc moved.rs
the compiler will abort the process with the error message moved_type.rs:8:3: 8:6 error: use of moved value: `vec` for line 8.
In Listing 1, the pointer vec
(line 6) generated in the main part of the program refers to the memory area in the heap that saves the representation of the vector with the components 1
, 2
, and 3
. Line 7 passes the pointer to print_length()
and makes the function the exclusive owner. As a consequence, the program can no longer use the pointer from line 7 onward, resulting in the error message.
Listing 1
Moved Value (moved_type.rs)
However, if you want to carry on using the pointer, you can temporarily lend it to print_length()
by passing in a "borrowed" pointer, &vec
, on line 7; also, the print_length()
function declaration in line 1 should be changed to vec: &Vec<i32>
. If print_length()
also needs to be able to change the pointer value, you can alternatively use the fragment vec: &mut Vec<i32>
.
However, Rust only allows developers to pass in a variable reference. If you want to handle resource borrowing correctly in all aspects of programming, Rust helps you do so with lifetimes [2]. Rust follows its own approach when releasing allocated memory: The Rust compiler releases memory automatically and does without a garbage collector as used in Java. The language also does not need to use the free()
function as in C and C++ to free memory programmatically.
Expressive
If Rust evaluates an expression, the return value is the result. However, all assignments have a return value: an empty tuple. As in many other languages, the semicolon acts as a grouping device to separate expressions and functions.
The following function determines the maximum of two integers, which it adds to the parameter list of the function named in the first line:
fn max(a:i32, b:i32) -> i32 { if a > b { return a }; b }
The max
function requires a 32-bit number as a data type for each parameter a
and b
. The return value to the right of the ->
operator is of the same type.
In the second line, the keyword return
(ret
in version 0.3) returns a
as the result. Otherwise, the next line returns the value b
. The last line of the function body does without a return
.
If the programmer were to mistakenly finish line 3 with a semicolon, the function would be invalid because that would convert the expression b
into an assignment. The return value type would thus be a tuple and not, as required, an integer.
Exemplary
Rust draws heavily on the repertoires of other languages. Like the function programming languages Haskell [3] and Standard ML [4], it uses patterns in various contexts. The assignment using the pattern (x,y,z)
on the left-hand side of the allocation
let (x,y,z) = (1,2,3);
declares the three variables x
, y
, and z
in one fell swoop. This assignment, also known as a destructuring assignment, assigns the value of 1
to x
, 2
to y
, and 3
to z
. The match
expression in
let w = match x { 0 => y+z, _ => y*z }
uses a pattern for case distinction to the left of the =>
operators. If the expression x
in the first case yields the value
, the variable w
stores the sum of y
and z
. In any other case, w
stores the product from the values of both variables.
Classless
Rust 1.0 scraps the concept of classes. Instead of class definitions, version 1.0 works with user-defined compound data types supplemented by methods, as required.
The keyword struct
introduces the definition of the user-defined data type container
in Listing 2. In line 2, an instance of container
stores a vector of 64-bit-wide floating-point numbers internally in content
.
Listing 2
Data Without Classes (impl.rs)
The block after the keyword impl
in line 5 defines the method that matches the Container
data type, such as print_content()
(lines 6-8). Thanks to the self
variable, it adopts a pointer to itself.
The next line indicates the number of floating-point numbers saved in content
using the macro println
to output via standard output in the shell. It also formats the results straightaway. However, before the macro does this, Rust replaces the placeholder {}
in the character string with the value of the len
method call for the vector from the content
component.
In the main part of the program, line 12 generates variable c
of type Container
. It then gets the vector – stored in the component content
– in line 13 after the colon. Line 16 also makes use of the print_content()
method with familiar dot notation. If a developer now compiles the file impl.rs
with
rustc impl.rs
and then executes it, the message The container stores 3 floats appears in the shell.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you've found an article to be beneficial.
News
-
2024 Open Source Professionals Job Survey Now Open
Share your expectations regarding open source jobs.
-
Arch Linux 2023.12.01 Released with a Much-Improved Installer
If you've ever wanted to install Arch Linux, now is your time. With the latest release, the archinstall script vastly simplifies the process.
-
Zorin OS 17 Beta Available for Testing
The upcoming version of Zorin OS includes plenty of improvements to take your PC to a whole new level of user-friendliness.
-
Red Hat Migrates RHEL from Xorg to Wayland
If you've been wondering when Xorg will finally be a thing of the past, wonder no more, as Red Hat has made it clear.
-
PipeWire 1.0 Officially Released
PipeWire was created to take the place of the oft-troubled PulseAudio and has finally reached the 1.0 status as a major update with plenty of improvements and the usual bug fixes.
-
Rocky Linux 9.3 Available for Download
The latest version of the RHEL alternative is now available and brings back cloud and container images for ppc64le along with plenty of new features and fixes.
-
Ubuntu Budgie Shifts How to Tackle Wayland
Ubuntu Budgie has yet to make the switch to Wayland but with a change in approaches, they're finally on track to making it happen.
-
TUXEDO's New Ultraportable Linux Workstation Released
The TUXEDO Pulse 14 blends portability with power, thanks to the AMD Ryzen 7 7840HS CPU.
-
AlmaLinux Will No Longer Be "Just Another RHEL Clone"
With the release of AlmaLinux 9.3, the distribution will be built entirely from upstream sources.
-
elementary OS 8 Has a Big Surprise in Store
When elementary OS 8 finally arrives, it will not only be based on Ubuntu 24.04 but it will also default to Wayland for better performance and security.