161390117 © Rolando Mayo | Dreamstime.com
Dreamstime L 161390117 Promo

Reading Rust for C Programmers

March 3, 2022
Here’s a very short intro to the syntax for Rust, the up-and-coming programming language in the embedded space.

This article is part ofTechXchange:Rusty Programming

What you’ll learn

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

Rust is the rising darling in the safe and secure programming arena. The challenge these days is that it’s changing as we speak, which will create 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 good idea to get to know Rust’s features, such as its memory management and approach to object-oriented programming.

Those are both major topics that I will push off to later articles. Their details are a bit more complex than what I want to do here, which is talk about some basic syntax that makes reading Rust code easier for the Rust novice.

Below is a short, runnable Rust program to generate Fibonnaci numbers. It highlights some interesting aspects of Rust that tend to be skipped when presenting Rust for the first time:

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); }

Thefnkeyword introduces a function or procedure. There’s additional syntax for function return values, but that’s for another time. The argument,n, is of typeusize.

Now for the interesting part. Theprintln!for printing is actually a macro namedprintln. The explanation point indicates that a macro is being invoked. Also, macros can have a variable number of arguments, while a function has a fixed number. The brackets in the format string, {}, are placeholders.

Rust supports tuples, i.e.(1, 1), that contain heterogenous elements, while arrays have homogenous elements. Both are fixed-length items; it’s possible to get the length, i.e.,arr.len(). The range, i.e.,0..n, in the for loop are a syntax common to other languages. The array element access looks likearr[i], while the tuple elements arex.1.

Theletstatement binds a value to a variable. Themutkeyword indicates that the variable is mutable, which is the default for Ada, C, and C++. Note: Non-mutable and constants are different. A non-mutable is one that the program can’t change at this point.

I should also talk about this function argument:arr: &mut [i32]. The ampersand indicates a reference that in this case is to an array of typei32that changed. On the other hand,constis used to define a constant that must be written as an upper-case name likeNUMBER.

最后,我们看起来像这样:

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

This defines an arrayxthat hasNUMBERof elements of typei32. The assigned value is an array of 0s. Of course, the code that’s generated will allocate the array in the stack and fill it with zeros before proceeding.

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

Read more articles in theTechXchange:Rusty Programming

Sponsored

4-V to 30-V input voltage, 1.5-A output current, synchronous buck LED driver with dimming options

4-V to 30-V input voltage, 1.5-A output current, synchronous buck LED driver with dimming options

TPSM82866A

TPSM82864A/TPSM82866A 2.4-V to 5.5-V Input, 4-A/6-A Step-Down Power Module with an Integrated Inductor in a 3.5-mm × 4.0-mm Thin Overmolded QFN Packa…

3.5-V to 65-V low Iq, dual, 2-phase synchronous buck DC-DC controller

3.5-V to 65-V low Iq, dual, 2-phase synchronous buck DC-DC controller

Fundamentals of Digital Potentiometers and Usages

May 19, 2021
机械电位器已经被设计使用ers for decades in applications ranging from circuit trimming to volume control. However, they have…

Voice your opinion!

This site requires you to register or login to post a comment.
No comments have been added yet. Want to start the conversation?
Baidu