Rust newbie here (<7 days into learning), the second hurdle I am trying to overcome after ownership rule is async/await.
I am writing a test that calls an async
function and I need to get the result from the Future
without using the keyword await
.
I have looked at async_test
, however I can't use that because (as I understand) this requires tokio
runtime and #[tokio_main]
attribute in my main
method - but I have my main
already decorated with #[actix_rt::main]
This is my test
#[test]
pub fn test_get_item() -> Result<(), anyhow::Error> {
let c = SomeClient::new();
let result = c.get_item(123456).await?; // <- this is not allowed
assert_eq!("Hello", result.title);
assert_eq!("https://example.com", result.url.as_str());
Ok(())
}
Things I have tried and failed (mostly due to my lack of knowledge in Rust)
- Use
async_test
on an actix web project using futures-await-test crate. - Read this thread in reddit.
- Follow few examples from this rust community thread
- Tried to
poll()
the future but it didn't lead me anywhere.
I don't understand why this has to be so complicated, maybe there is a simple function (like wait()
or get_result()
) somewhere for Future
?
Thanks for your help.