async/await

En general, el código asíncrono de Rust se parece mucho al código secuencial “normal”:

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Speaker Notes

This slide should take about 6 minutes.

Puntos clave:

  • Ten en cuenta que este es un ejemplo simplificado para mostrar la sintaxis. No hay ninguna operación de larga duración ni concurrencia real.

  • ¿Cuál es el tipo de resultado devuelto de una llamada asíncrona?

    • Consulta el tipo con let future: () = async_main(10); en main .
  • The “async” keyword is syntactic sugar. The compiler replaces the return type with a future.

  • No se puede hacer que main sea asíncrono sin dar instrucciones adicionales al compilador sobre cómo usar el futuro devuelto.

  • You need an executor to run async code. block_on blocks the current thread until the provided future has run to completion.

  • .await espera de forma asíncrona la finalización de otra operación. A diferencia de block_on, .await no bloquea el hilo.

  • .await can only be used inside an async function (or block; these are introduced later).