Implement ‘unimplemented’ feature of Rust

struct
Reading Time: 2 minutes

The headline looks weird. Isn’t it. Well, there are some hidden corals in Rust sea, which you won’t find in Rust documentation. In the continuation of my exploration about Rust language, I found a useful feature unimplemented!.

This feature lets you write a not-yet-implemented method, like this:

fn doSomething() {
    unimplemented!()
}

The methods you define can also take input parameters and specify a return type, like this:

fn fibonacci(n: u32) -> u32 {
    unimplemented!()
}

Although your code will compile if you call this method, it will crash with a panic message not yet implemented.

This is really nice for when you want to stub out some methods as you’re working on a problem. For example, if you are working on an IoT application to control an office door, and you know you’re going to need methods to open and close the door, but you don’t know the details yet. In this case, you can stub out your methods like this:

trait DoorController {
    fn open(&self);
    fn close(&self);
}

struct MyOffice;

impl DoorController for MyOffice {
    fn open(&self) {
        unimplemented!()
    }
    fn close(&self) {
        unimplemented!()
    }
}

This is helpful when you want to write code using a particular function without having to write the entire implementation. I hope you enjoy reading this blog. Thanks !


Knoldus-blog-footer-image

Written by 

Ayush is the Sr. Lead Consultant @ Knoldus Software LLP. In his 10 years of experience he has become a developer with proven experience in architecting and developing web applications. Ayush has a Masters in Computer Application from U.P. Technical University, Ayush is a strong-willed and self-motivated professional who takes deep care in adhering to quality norms within projects. He is capable of managing challenging projects with remarkable deadline sensitivity without compromising code quality.