Rust – Interface
Last Updated :
11 Oct, 2022
Rust is a systems programming language with a concept of Interfaces in Rust that is implemented by Traits.Trait in Rust is basically a struct having no bodies for the methods. The logic is generally not defined but only the methods are. Rust enforces us to follow a set of coding practices with implementing the interface. Anyone having a background in OOP languages like C++ or Java can easily draw an analogy between interfaces in OOPS and Rust’s way of implementing interface as a trait. Interfaces in Rust help in building ‘Has A relationship’ applications as they are loosely coupled.
Syntax:
Abstract methods are empty methods while Concrete methods are regular methods
trait NameofTrait {
// defining Empty method
fn name_of_method (&self); //self parameter
// defining regular method
fn name_of_method (&self); //self parameter
{
// method body
}
}
Trait methods have &self parameter defined as the first parameter for the methods. For specifying any extra parameters, we need to specify them after the self-parameter declaration.
Defining a Trait as an interface:
fn main() {}
// defining a trait -GFG
trait GeeksforGeeks {
fn gfg(&self);
}
Here, the Trait GeeksforGeeks is implemented with an abstract method (single defined).
Example 1:
Rust
fn main() {}
struct Courses {}
struct DSAPractice {}
trait GeeksforGeeks {
fn gfg(& self );
}
impl GeeksforGeeks for Courses {
fn gfg(& self ) {
println!( "Lets buy GFG courses " );
}
}
impl GeeksforGeeks for DSAPractice {
fn gfg(& self ) {
println!( "Lets practice dsa on GFG " );
}
}
|
Output:
In this example, we define two structs Courses and DSAPractice. Then, we define a trait GeeksforGeeks and also nest a function gfg inside it with self parameter. Implementing GeeksforGeeks traits for Courses and DsaPractice Traits.
Example 2:
Rust
fn main() {
let courses = Courses {};
let practice = Practice {};
courses.gfg();
practice.gfg();
}
struct Courses {}
struct Practice {}
trait GeeksforGeeks {
fn gfg (& self );
}
impl GeeksforGeeks for Courses {
fn gfg(& self ) {
println!( "Buying courses from GFG" );
}
}
impl GeeksforGeeks for Practice {
fn gfg(& self ) {
println!( "Practicing DSA on GFG" );
}
}
|
Output:
Explanation:
In this example, we declare structs for courses and practice and then implement the traits Geeksforgeeks for structs Practice and Courses.
Example 3:
Rust
fn main() {
let courses = Courses {};
let practice = Practice {};
courses.gfg();
courses.otherwebsites();
practice.gfg();
}
struct Courses {}
struct Practice {}
trait GeeksforGeeks {
fn gfg(& self );
}
trait OtherWebsites {
fn otherwebsites(& self );
}
impl GeeksforGeeks for Courses {
fn gfg(& self ) {
println!( "GFG Courses are best" );
}
}
impl OtherWebsites for Courses {
fn otherwebsites(& self ) {
println!( "Courses of other websites are not that good" );
}
}
impl GeeksforGeeks for Practice {
fn gfg(& self ) {
println!( "Practicing DSA is best on GFG" );
}
}
|
Output:
Explanation:
In the example above, we add a trait called OtherWebsites with a method of other websites(). Here, we call courses.otherwebsites() method. Next, we define the traits GeeksforGeeks and other websites and pass (&self) parameters in them as well. Next, use the impl function to declare the traits.
Share your thoughts in the comments
Please Login to comment...