Rust, Software Development

Function Accepts Struct That Implements A Trait

Rust lang Convert Struct JSON

This post is about functions in Rust accepting struct instances that implement a specific trait. We will start off with simple Java codes that implements Java interfaces. Next, we will convert the Java codes to Rust keeping the same functionality.

Function, Struct, and Trait From Java Perspective

If this was for Java, the title would be “Method Accepts Class That Implements An Interface.” Still, we should not forget that Java is different from Rust.

Java Interfaces

We would have the following interfaces to implement. Any class that implements the Bounceable and Inflatable interfaces has the methods bounce  and inflate.

Java Interface Implementations

We have two classes – BasketBall and TennisBall. Both balls bounce, but only one of them is inflatable. Consider the following codes. The BasketBall class implements both the Bounceable and Inflatable interfaces, but the TennisBall only implements  Bounceable.

Usage

There are two methods – toss and inflate. The codes can pass BasketBall instances to both methods but can only pass TennisBall instances to the toss method.

The codes generate this output.

If we uncomment the last time, we will get a compilation error. The error means that the inflate method cannot accept any class that does not implement the Inflatable interface. If we look at the TennisBall class, it implements the Bounceable interface, not the Inflatable interface – thus the error message “Incompatible types: TennisBall cannot be converted to Inflatable.”

"Function Accepts Struct That Implements A Trait" in Java

In Rust – Generic Function With Generic Parameter That Implements A Trait

Rust Traits

Rust does not have interfaces. Instead, they have traits. Traits are no way similar to Java interfaces – just to be clear! In this section, we are essentially porting the Java codes to Rust.

Rust Trait Implementations

First, we make BasketBall both Bouceable and Inflatable.

Second, we make TennisBall Inflatable only.

Usage

We have generic functions that accept structs of  T parameter type.

The codes generate the following output.

The codes are now very similar to the Java codes. We successfully ported the functionality to Rust.

If we try to uncomment the last time, we will have a compilation error.  The error means that the inflate method cannot accept any struct that does not implement the Inflatable trait. If we look at the TennisBall struct, it implements  the Bounceable trait, not the Inflatable trait – thus the error message “the trait Inflatable is not implemented for TennisBall.”

In summary, we can create generic functions in Rust that accepts any struct instances that implements a particular trait with the help of generics. Although we use Java codes for demonstration, Rust traits are, by no means, similar to Java interfaces.

We tested the codes using Rust 1.39.0.

Errata

  1. Changed self to &self. Using &self  is recommended to avoid error[E0382]  when calling other functions in the same instance. For example:

Got comments, or suggestions? Please visit our Facebook page!

You Might Also Like