Read Rust for C Programmers

0

This article is part of TechXchange: Rusty Programming

What you will learn

  • Some basic Rust syntax.
  • Differences between Rust and C/C++.

Rust is the rising darling of safe and secure programming. The challenge these days is that this is changing right now, which will wreak havoc on embedded applications, especially those that require long-term support. It will likely join Ada, C, and C++ in the future as it continues to be refined. That’s why it’s a good idea to familiarize yourself with the features of Rust, such as its memory management and its approach to object-oriented programming.

These are two major topics that I will postpone in later articles. Their details are a bit more complex than what I want to do here, which talks about a basic syntax that makes Rust code easier to read for the Rust novice.

Below is a short, executable Rust program for generating Fibonacci numbers. It highlights some interesting aspects of Rust that tend to be overlooked when first introducing Rust:

fn print_fib_1(n: usize) {
    println!("Fibonacci 1");
    let mut x = (1, 1);
    for i in 0..n {
        println!("{}: {}", i, x.0);
        x = (x.1, x.0 + x.1)
    }
}

fn print_fib_2( arr: &mut [i32]) {
    println!("Fibonacci 2");
    arr[0] = 1;
    arr[1] = 1;
    let n = arr.len();

    for i in 2..n {
        arr[i] = arr[i-1] + arr[i-2];
    }

    for i in 0..n {
        println!("{}: {}", i, arr[i]);
    }
}

fn main() {
    const NUMBER: usize = 5;
    println!("Hello, Fibonacci!");
    print_fib_1(NUMBER);

    let mut x:[i32; NUMBER] = [0; NUMBER];
    print_fib_2(&mut x);
}

the fn The keyword introduces a function or a procedure. There is additional syntax for function return values, but that’s for another time. The argument, notis of type use.

Now let’s get to the interesting part. the print! for printing is actually a macro named println. The explanation period indicates that a macro is called. Also, macros can have a variable number of arguments, while a function has a fixed number. The square brackets in the format string, {}, are placeholders.

Rust supports tuples, i.e. (1, 1), which contain heterogeneous elements, while arrays have homogeneous elements. Both are fixed-length elements; it is possible to get the length, i.e., arr.len(). The range, i.e. 0..n, in the for loop is a syntax common to other languages. Accessing array elements looks like arr[i]while tuple elements are x.1.

the to leave The statement binds a value to a variable. the mute The keyword indicates that the variable is modifiable, which is the default for Ada, C and C++. Note: Non-modifiable variables and constants are different. A non-mutable is one that the program cannot change at this point.

I should also talk about this function argument: arr: &mut [i32]. The ampersand indicates a reference, which in this case is to an array of type i32 it has changed. On the other hand, constant is used to define a constant that should be written as an uppercase name like NUMBER.

Finally, we have something that looks like this:

let mut x:[i32; NUMBER] = [0; NUMBER];

This defines an array X who has NUMBER type elements i32. The assigned value is an array of 0s. Of course, the generated code will allocate the array in the stack and fill it with zeros before continuing.

There are a lot of new ideas in Rust, or ones that seem different from other languages. Knowing what you’re looking at will help you better understand Rust when you start coding. I hope that helps.

Read more articles in the TechXchange: Rusty programming

Share.

Comments are closed.