January 2019
Intermediate to advanced
520 pages
14h 32m
English
Some programming languages, such as JavaScript and C#, have async and await operators which help to write asynchronous code that looks like synchronous code. The nightly version of the Rust compiler supports a new syntax and adds async and await (actually, this is a macro) keywords to the language to simplify the writing of asynchronous applications. The new code might look as follows:
async fn http_get(addr: &str) -> Result<String, std::io::Error> { let mut conn = await!(NetwrokStream::connect(addr))?; let _ = await!(conn.write_all(b"GET / HTTP/1.0\r\n\r\n"))?; let mut buf = vec![0;1024]; let len = await!(conn.read(&mut buf))?; let res = String::from_utf8_lossy(&buf[..len]).to_string(); Ok(res)}
This is not stable ...
Read now
Unlock full access