Checked the `Output` of this `async fn`, found opaque type

When i try to return this asynchronous function it gives this error

use std;
use serde_json::Value;
use chrono::Chrono;
mod constants;

fn main {
   let api_public_key = std::env::var(constants::API_PUBLIC_KEY_ENV_VAR).unwrap();
   let api_secret_key = std::env::var(constants::API_SECRET_KEY_ENV_VAR).unwrap();
   let chrono: chrono = Chrono::new(&api_public_key, &api_secret_key);


async fn initialize_payment (
    chrono: Chrono,
    reference: String, 
    amount: String,
    customer_name: String,
    customer_email: String, 
    currency: String
    ) -> Value {
error[E0308]: mismatched types
  --> src/
6  |   fn main() {
   |             - expected `()` because of default return type
12 | /     initialize_payment(
13 | |         chrono, 
14 | |         "Y8c8wH55l0435t3545435".to_string(),
15 | |         "1000".to_string(),
16 | |         "Name".to_string(),
17 | |         "Email".to_string(),
18 | |         "USD".to_string(),
20 | |     )
   | |     ^- help: consider using a semicolon here: `;`
   | |_____|
   |       expected `()`, found opaque type
note: while checking the return type of the `async fn`
  --> src/
31 |     ) -> Value {
   |          ^^^^^ checked the `Output` of this `async fn`, found opaque type
   = note: expected unit type `()`
            found opaque type `impl Future<Output = Value>`

For more information about this error, try `rustc --explain E0308`.
error: could not compile `example` due to previous error

The code and error you have posted makes it rather difficult to help you. The code is clearly not valid Rust code, so it is clear that you have changed it and that the code you posted is not the code that the error comes from — the source of the error could have been something you changed in the progress of creating the snippet you posted. Additionally, the error message appears copied from an IDE popup rather than being the full output as produced by cargo build or cargo check. Those commands generally provide error messages with a lot more detail than IDE popups do, and you should always post the full error.

Anyway, my guess is that you're missing an await in the last statement of your async fn. Maybe you're also missing the return type.

1 Like

Thank you for your response, I have updated the code and the error message.

You will need to add the #[tokio::main] macro so that your main function becomes async. (Alternatively, create a Runtime explicitly - see the documentation for the #[tokio::main] macro for details)

It didn't work, it produces this

    Finished dev [unoptimized + debuginfo] target(s) in 2.16s
     Running `target/debug/example`
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: NotPresent', src/
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Did you set the environment variables which names are stored in constants::API_PUBLIC_KEY_ENV_VAR and constants::API_SECRET_KEY_ENV_VAR?

1 Like

It absolutely did work. You fixed the error you had previously, and now you get a new, unrelated error.

1 Like

Yes it did thank you, I should have rephrased my response!

I just realized the issue was from my environment variables not being set correctly!

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.