diff --git a/develop-docs/engineering-practices/rust.mdx b/develop-docs/engineering-practices/rust.mdx index 8118e62c79d1f..aa1cae45b40fb 100644 --- a/develop-docs/engineering-practices/rust.mdx +++ b/develop-docs/engineering-practices/rust.mdx @@ -50,26 +50,26 @@ During migration you may need normal functions which return futures, for their s ### Async Traits -In **traits** you can not yet use `async fn` ([see this blog post](https://smallcultfollowing.com/babysteps/blog/2019/10/26/async-fn-in-traits-are-hard/)). -In this case, functions should return `-> Pin + Send>>`: +Support for async in **traits** has [landed in Rust](https://blog.rust-lang.org/2023/12/21/async-fn-rpit-in-traits.html) +and should generally be preferred now. ```rust -trait Database { - fn get_user(&self) -> Pin + Send + '_>>; +pub trait Database { + fn get_user(&self) -> impl Future + Send; } -impl Database for MyDB { - fn get_user(&self) -> Pin + Send + '_>> { - Box::pin(async { - // ... - }) - } +impl Database for MyDatabase { + async fn get_user(&self) -> User { + todo!() + } } ``` Note that the returned future type is `Send`, to ensure that it can run on a multi-threaded runtime. -This corresponds to what the [async-trait crate](https://crates.io/crates/async-trait) does. +When you need dynamic dispatch or have to support Rust versions older than 1.75 consider using the +[`async-trait`](https://docs.rs/async-trait/) crate. + ### Avoid `.unwrap()`