Hello everyone. I am a (mostly) Golang programmer trying to dabble into Rust.
I am very much familiar with Go Goroutines/Channels concept for multithreading and such. However, Rust does not seem to follow this pattern. Instead, it follows the Async/Await model of JS. This got me confused
- Can somebody do an ELI5 for me on the main/biggest/fundamental difference between the two paradigms, at least in term of implementation in Golang and Rust. Their pros/cons...etc.
- How do I execute an async function in a non-async function? Or is my programming paradigm is wrong with Rust?
For example, I want to run this function from a cratepublic_ip
to, well, get IP.
async fn get_ip() {
// Attempt to get an IP address and print it.
if let Some(ip) = public_ip::addr().await {
println!("public ip address: {:?}", ip);
} else {
println!("couldn't get an IP address");
}
}
If I throw this function into my main
, for sure I will come across:
fn main() {
| ---- this is not `async`
21 | get_ip.await;
| ^^^^^^ only allowed inside `async` functions and blocks
Do I need to turn the main function into async? Do I need to put and async
to function main
? Does that make my entire Rust code become asynchronous, like Javascript? Because honestly I am not that familiar with asynchronous programming.
3. Finally, why this way? Like why did Rust choose the paradigm of Async/Await instead of multithreading like Golang? I tried to search for a few Webs Framework, and fundamentally, as far as I can understand, Actix Web, Rocket, Warp and Tide... all developed under the Async/Await pattern, so that makes me wonder is there a really deep reason why Rust was developed this way. And also, is there any Web Framework that follows multithreads paradigm like that of Go?
Thanks in advance.