Zero cost abstraction,methods and rust

In a low power microcontroller,It seems that method calls can worth a great deal.
I wonder if zero cost abstraction apply to methods in rust.
Will using methods(instead of using functions) affect the performance of my low end microcontroller?

Methods are just functions with special first argument. And they will be inlined in the same way - that is, wherever it's appropriate from optimization point of view.

2 Likes

I think one notable exception are methods of trait objects, where the type has to be determined at runtime.

But using generics (e.g. when calling Vec<T>::push, for example), code can be inlined. That is a huge difference to many other languages, which suffer from unwanted type erasure (also see generics in Java).

Dynamic calls can be inlined too, especially when building an executable (so that all possible types are known to the compiler). The technique is called de-virtualization.

3 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.