[rust-dev] Porting a small DSP test from C++ to Rust: Comments and performance observations

Vladimir Matveev dpx.infinity at gmail.com
Wed Jun 11 00:09:03 PDT 2014

Hi, Kevin!

> * What would be the replacement for a struct-scoped static constant, so I could put a static inside a struct instead of making it a global?
It is not possible now. There are some suggestions on associated items, but I don’t think they are active. Currently Rust module system is used to control scopes of statics.

> * Rust doesn't have prefix/postfix increment? Or, I just didn't find the right syntax of using it?
Yes, Rust doesn’t have it. You should use composite assignment: x += 1. It is not an expression, though.

> * My biggest problem was figuring out how to use arrays. Originally, things just weren't working and I think it's because I was inadvertently copying an array instead of referring to the original. t just couldn't figure out how to create a mutable alias to an array passed into a function by reference.
Well, you have correctly figured out that it is done using slices :)

> * I understand the reasoning behind explicit integer conversions, but depending on what one is doing, it can add to a lot of explicit conversions, and I also didn't figure out a way to do an unsigned for loop.
Yes, explicit conversions may sometimes be too verbose. As for unsigned for loop, it is easy. Remember, Rust uses type inference to find out correct types of all local variables. `range()` function which creates range iterators is generic and looks like this:

    fn range<T>(from: T, until: T) -> Range<T> { … }

(actual definition is different because T is not arbitrary but bounded with some traits)

The `T` type parameter is determined automatically from the use site of the function. In your case it is deduced as `int` because  of `length` variable (which is of `int` type). So you can just cast `length` to `uint`:

    for i in range(0, length as uint) { … }

and `i` variable will be unsigned. BTW, why did you define `length` parameter as `int` at all? You can make it `uint` and you won’t need to do this cast.

> * When creating / using arrays, there is sometimes duplication of the size parameter. Is there a way to reduce that?
I don’t think so. They are statically sized arrays, so they just need their size specified. When you don’t care about their size, you usually use slices anyway.

More information about the Rust-dev mailing list