12

Implement 'unimplemented' feature of Rust - Knoldus Blogs

 3 years ago
source link: https://blog.knoldus.com/implement-unimplemented-feature-of-rust/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Implement 'unimplemented' feature of Rust

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 !


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK