Return Result from trait

⚓ rust    📅 2025-06-06    👤 surdeus    👁️ 4      

surdeus

Info

This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: Return Result from trait

Hello,

I'm having a bit of trouble with Rust traits and I was hoping I could get some help from the community.

I want to create a trait with a function that returns a Result. The Result should be of the type Result<String, String> but this doesn't seem to be possible as the code I've implemented (below) gives the following error.

Would anyone be able to explain what I am doing wrong here?

Error

method do_something has an incompatible type for trait expected signature fn() -> Result<(), std::fmt::Error> found signature fn() -> Result<String, String>

Code

pub trait Operations {
    fn do_something() -> Result;
}

impl Operations for Node {
    fn do_something() -> Result<String, String> {
        // Do some stuff
    }
}

5 posts - 4 participants

Read full topic

🏷️ rust_feed