Return Result from trait
⚓ Rust 📅 2025-06-06 👤 surdeus 👁️ 17Hello,
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_somethinghas an incompatible type for trait expected signaturefn() -> Result<(), std::fmt::Error>found signaturefn() -> 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
🏷️ rust_feed