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.
Similar Reads
Rust - Literals A literal is a source code that represents a fixed value and can be represented in the code without the need for computation. The compiler uses by default i32 for integers and f64 for float types/. In Rust, literals are described by adding them in the type as a suffix. Example:  Integer literal 6 h
1 min read
Rust - Generics Generics in Rust is a method of generalizing data types. Generics are always defined at run time. As generics can have multiple forms over given parameters and can be applied to methods, functions, structures, traits, etc they are often referred to as parametric polymorphism in type theory. Generics
2 min read
SAP ABAP | Interfaces ABAP(Advanced Business Application Programming) is an object-oriented programming language that supports many oops concepts like other programming languages. It supports all the four pillars of oops i.e. Inheritance, Polymorphism, Abstraction, and Encapsulation. The interface is one of the oops conc
4 min read
Rust - Iterator Trait Rust is a systems programming language focusing on speed, concurrency, and ensuring safe code. The iterator trait in Rust is used to iterate over Rust collections like arrays. Syntax: //pub refers to the trait being declared public pub trait Iterator { // type refers to the elements type over which
3 min read
Rust - Lifetime Lifetimes in Rust refer to the lifetime of how long a variable would live. Lifetimes are associated with references as references cannot live longer than the object from which it is derived. Lifetimes basically describe the scope that a reference is valid for. Lifetimes come into play when we are de
3 min read