Implement trait bounds for trait X

can someone help me understand why these kind of errors occur and how can i solve these . reference code

use std::{process::Command, clone};
use serde;


#[tokio::main]
async fn main() { 
let cmd = get_job().await;
print!("{:#?}",cmd);
}

async fn get_job() -> Result<String, Box<dyn std::error::Error>> {
    let client = reqwest::Client::new();
    let job = client.get("http://192.168.29.210:5000/def/").send()
        .await?
        .text()
        .await?;

    Ok(job)
}


async fn exec(){
    let mut cmd= get_job().await;
    let mut j = String::from(cmd);

    let output = if cfg!(target_os = "windows") {
        Command::new("cmd")
                .args(["/C",])
                .output()
                .expect("failed to execute process")
    } else {
        Command::new("sh")
                .arg("-c")
                .arg()
                .output()
                .expect("failed to execute process")
    };
    
    let hello = output.stdout;
}

error :
line 39 ^ the trait AsRef<OsStr> is not implemented for Result<std::string::String, Box<dyn StdError>>
line 24 the trait From<Result<std::string::String, Box<dyn StdError>>> is not implemented for `std::string::String

Did you post the wrong code? The error message from the code is different from what you posted. In the code you posted, you're calling String::from(cmd) where cmd's type is Result<String, Box<dyn std::error::Error>>. You can only String::from with types that have a From implementation for String. This implementation doesn't exist for Result, so you get this error:

error[E0277]: the trait bound `std::string::String: From<Result<std::string::String, Box<dyn StdError>>>` is not satisfied
  --> src/main.rs:24:30
   |
24 |     let mut j = String::from(cmd);
   |                 ------------ ^^^ the trait `From<Result<std::string::String, Box<dyn StdError>>>` is not implemented for `std::string::String`
   |                 |
   |                 required by a bound introduced by this call

I recommend reading the chapter on Result from the book if you haven't yet: Recoverable Errors with Result - The Rust Programming Language Long story short, you need to handle the errors that can occur during get_job to get the string out of it.

There's also another error regarding .arg(), which can't be called without any arguments, but that's probably just a typo since you're using it correctly above it.

thanks for replying . the cmd was supposed to be passed to the arg but i keep getting "trait is not satisfied for the bound" errors

Please look at the signature of your functions. You'll see that wherever you assumed to have a String, you in fact have a Result<String, …> instead. Those are two different types. You have to get the string ot of the result – i.e., you have to handle errors somehow.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.