Open In App

Rust – Interface

Last Updated : 11 Oct, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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




// Rust program of defining a Trait and
// implementing it as an interface:
fn main() {}
 
// structs
struct Courses {}
struct DSAPractice {}
 
 
// trait
trait GeeksforGeeks {
    fn gfg(&self);
}
 
// implement trait for structs
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




// Rust program for Implementing
// Trait methods as an interface
fn main() {
 
    let courses = Courses {};
    let practice = Practice {};
 
    courses.gfg();
    practice.gfg();
 
}
 
// structs defined are Courses and Practice
struct Courses {}
struct Practice {}
 
 
// trait
trait GeeksforGeeks {
    fn gfg (&self);
}
 
// implement trait for structs
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




// Rust program for Implementing
// multiple methods as Interfaces
fn main() {
 
    let courses = Courses {};
    let practice = Practice {};
 
    courses.gfg();
    courses.otherwebsites();
    practice.gfg();
 
}
 
// structs
struct Courses {}
struct Practice {}
 
 
// trait
trait GeeksforGeeks {
    fn gfg(&self);
}
trait OtherWebsites {
    fn otherwebsites(&self);
}
 
// implement traits for Courses
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");
    }
}
 
// implement trait for plane
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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads