├── README.md ├── Cargo.toml ├── src └── main.rs └── Cargo.lock /README.md: -------------------------------------------------------------------------------- 1 | # sandbox-async-rust 2 | A sandbox I played around in for this [article](https://phillip-england.com/post/async-rust). 3 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "sandbox-async-rust" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | [dependencies] 7 | tokio = { version = "1", features = ["full"] } 8 | rand = "0.8.5" 9 | -------------------------------------------------------------------------------- /src/main.rs: -------------------------------------------------------------------------------- 1 | use std::time::Duration; 2 | use tokio::time::sleep; 3 | use tokio::sync::Mutex; 4 | use std::sync::Arc; 5 | 6 | #[tokio::main] 7 | async fn main() { 8 | basic_async_await("yo".to_string()).await; 9 | futures_are_lazy().await; 10 | morph_data_across_threads().await; 11 | 12 | 13 | 14 | 15 | 16 | } 17 | 18 | 19 | // how to use basic async/await 20 | async fn basic_async_await(msg: String) { 21 | println!("I am an async function! Here is my message: {}", msg); 22 | } 23 | 24 | // how to get a future, and await it later 25 | async fn futures_are_lazy() { 26 | let fut = basic_async_await("lazyyyy".to_string()); 27 | fut.await; 28 | } 29 | 30 | // how to morph data across multiple threads 31 | async fn morph_data_across_threads() { 32 | let str = String::from("I will be morphed!"); 33 | let str_arc = Arc::new(Mutex::new(str)); 34 | let mut handles = vec![]; 35 | for i in 0..10 { 36 | let str_clone = Arc::clone(&str_arc); 37 | let task = tokio::spawn(async move { 38 | // lock the mutex to modify it 39 | let mut val = str_clone.lock().await; 40 | val.push_str(&i.to_string()); 41 | }); 42 | handles.push(task); 43 | } 44 | for task in handles { 45 | task.await.unwrap(); 46 | } 47 | // retrieve the final value as str is expended 48 | let final_str = str_arc.lock().await; 49 | println!("{}", final_str); 50 | } 51 | 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 3 4 | 5 | [[package]] 6 | name = "addr2line" 7 | version = "0.24.2" 8 | source = "registry+https://github.com/rust-lang/crates.io-index" 9 | checksum = "dfbe277e56a376000877090da837660b4427aad530e3028d44e0bffe4f89a1c1" 10 | dependencies = [ 11 | "gimli", 12 | ] 13 | 14 | [[package]] 15 | name = "adler2" 16 | version = "2.0.0" 17 | source = "registry+https://github.com/rust-lang/crates.io-index" 18 | checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627" 19 | 20 | [[package]] 21 | name = "autocfg" 22 | version = "1.3.0" 23 | source = "registry+https://github.com/rust-lang/crates.io-index" 24 | checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" 25 | 26 | [[package]] 27 | name = "backtrace" 28 | version = "0.3.74" 29 | source = "registry+https://github.com/rust-lang/crates.io-index" 30 | checksum = "8d82cb332cdfaed17ae235a638438ac4d4839913cc2af585c3c6746e8f8bee1a" 31 | dependencies = [ 32 | "addr2line", 33 | "cfg-if", 34 | "libc", 35 | "miniz_oxide", 36 | "object", 37 | "rustc-demangle", 38 | "windows-targets", 39 | ] 40 | 41 | [[package]] 42 | name = "bitflags" 43 | version = "2.6.0" 44 | source = "registry+https://github.com/rust-lang/crates.io-index" 45 | checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" 46 | 47 | [[package]] 48 | name = "byteorder" 49 | version = "1.5.0" 50 | source = "registry+https://github.com/rust-lang/crates.io-index" 51 | checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" 52 | 53 | [[package]] 54 | name = "bytes" 55 | version = "1.7.1" 56 | source = "registry+https://github.com/rust-lang/crates.io-index" 57 | checksum = "8318a53db07bb3f8dca91a600466bdb3f2eaadeedfdbcf02e1accbad9271ba50" 58 | 59 | [[package]] 60 | name = "cfg-if" 61 | version = "1.0.0" 62 | source = "registry+https://github.com/rust-lang/crates.io-index" 63 | checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" 64 | 65 | [[package]] 66 | name = "getrandom" 67 | version = "0.2.15" 68 | source = "registry+https://github.com/rust-lang/crates.io-index" 69 | checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" 70 | dependencies = [ 71 | "cfg-if", 72 | "libc", 73 | "wasi", 74 | ] 75 | 76 | [[package]] 77 | name = "gimli" 78 | version = "0.31.1" 79 | source = "registry+https://github.com/rust-lang/crates.io-index" 80 | checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f" 81 | 82 | [[package]] 83 | name = "hermit-abi" 84 | version = "0.3.9" 85 | source = "registry+https://github.com/rust-lang/crates.io-index" 86 | checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" 87 | 88 | [[package]] 89 | name = "libc" 90 | version = "0.2.168" 91 | source = "registry+https://github.com/rust-lang/crates.io-index" 92 | checksum = "5aaeb2981e0606ca11d79718f8bb01164f1d6ed75080182d3abf017e6d244b6d" 93 | 94 | [[package]] 95 | name = "lock_api" 96 | version = "0.4.12" 97 | source = "registry+https://github.com/rust-lang/crates.io-index" 98 | checksum = "07af8b9cdd281b7915f413fa73f29ebd5d55d0d3f0155584dade1ff18cea1b17" 99 | dependencies = [ 100 | "autocfg", 101 | "scopeguard", 102 | ] 103 | 104 | [[package]] 105 | name = "memchr" 106 | version = "2.7.4" 107 | source = "registry+https://github.com/rust-lang/crates.io-index" 108 | checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" 109 | 110 | [[package]] 111 | name = "miniz_oxide" 112 | version = "0.8.0" 113 | source = "registry+https://github.com/rust-lang/crates.io-index" 114 | checksum = "e2d80299ef12ff69b16a84bb182e3b9df68b5a91574d3d4fa6e41b65deec4df1" 115 | dependencies = [ 116 | "adler2", 117 | ] 118 | 119 | [[package]] 120 | name = "mio" 121 | version = "1.0.2" 122 | source = "registry+https://github.com/rust-lang/crates.io-index" 123 | checksum = "80e04d1dcff3aae0704555fe5fee3bcfaf3d1fdf8a7e521d5b9d2b42acb52cec" 124 | dependencies = [ 125 | "hermit-abi", 126 | "libc", 127 | "wasi", 128 | "windows-sys", 129 | ] 130 | 131 | [[package]] 132 | name = "object" 133 | version = "0.36.5" 134 | source = "registry+https://github.com/rust-lang/crates.io-index" 135 | checksum = "aedf0a2d09c573ed1d8d85b30c119153926a2b36dce0ab28322c09a117a4683e" 136 | dependencies = [ 137 | "memchr", 138 | ] 139 | 140 | [[package]] 141 | name = "parking_lot" 142 | version = "0.12.3" 143 | source = "registry+https://github.com/rust-lang/crates.io-index" 144 | checksum = "f1bf18183cf54e8d6059647fc3063646a1801cf30896933ec2311622cc4b9a27" 145 | dependencies = [ 146 | "lock_api", 147 | "parking_lot_core", 148 | ] 149 | 150 | [[package]] 151 | name = "parking_lot_core" 152 | version = "0.9.10" 153 | source = "registry+https://github.com/rust-lang/crates.io-index" 154 | checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8" 155 | dependencies = [ 156 | "cfg-if", 157 | "libc", 158 | "redox_syscall", 159 | "smallvec", 160 | "windows-targets", 161 | ] 162 | 163 | [[package]] 164 | name = "pin-project-lite" 165 | version = "0.2.15" 166 | source = "registry+https://github.com/rust-lang/crates.io-index" 167 | checksum = "915a1e146535de9163f3987b8944ed8cf49a18bb0056bcebcdcece385cece4ff" 168 | 169 | [[package]] 170 | name = "ppv-lite86" 171 | version = "0.2.20" 172 | source = "registry+https://github.com/rust-lang/crates.io-index" 173 | checksum = "77957b295656769bb8ad2b6a6b09d897d94f05c41b069aede1fcdaa675eaea04" 174 | dependencies = [ 175 | "zerocopy", 176 | ] 177 | 178 | [[package]] 179 | name = "proc-macro2" 180 | version = "1.0.86" 181 | source = "registry+https://github.com/rust-lang/crates.io-index" 182 | checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" 183 | dependencies = [ 184 | "unicode-ident", 185 | ] 186 | 187 | [[package]] 188 | name = "quote" 189 | version = "1.0.37" 190 | source = "registry+https://github.com/rust-lang/crates.io-index" 191 | checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" 192 | dependencies = [ 193 | "proc-macro2", 194 | ] 195 | 196 | [[package]] 197 | name = "rand" 198 | version = "0.8.5" 199 | source = "registry+https://github.com/rust-lang/crates.io-index" 200 | checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" 201 | dependencies = [ 202 | "libc", 203 | "rand_chacha", 204 | "rand_core", 205 | ] 206 | 207 | [[package]] 208 | name = "rand_chacha" 209 | version = "0.3.1" 210 | source = "registry+https://github.com/rust-lang/crates.io-index" 211 | checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" 212 | dependencies = [ 213 | "ppv-lite86", 214 | "rand_core", 215 | ] 216 | 217 | [[package]] 218 | name = "rand_core" 219 | version = "0.6.4" 220 | source = "registry+https://github.com/rust-lang/crates.io-index" 221 | checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" 222 | dependencies = [ 223 | "getrandom", 224 | ] 225 | 226 | [[package]] 227 | name = "redox_syscall" 228 | version = "0.5.4" 229 | source = "registry+https://github.com/rust-lang/crates.io-index" 230 | checksum = "0884ad60e090bf1345b93da0a5de8923c93884cd03f40dfcfddd3b4bee661853" 231 | dependencies = [ 232 | "bitflags", 233 | ] 234 | 235 | [[package]] 236 | name = "rustc-demangle" 237 | version = "0.1.24" 238 | source = "registry+https://github.com/rust-lang/crates.io-index" 239 | checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" 240 | 241 | [[package]] 242 | name = "sandbox-async-rust" 243 | version = "0.1.0" 244 | dependencies = [ 245 | "rand", 246 | "tokio", 247 | ] 248 | 249 | [[package]] 250 | name = "scopeguard" 251 | version = "1.2.0" 252 | source = "registry+https://github.com/rust-lang/crates.io-index" 253 | checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" 254 | 255 | [[package]] 256 | name = "signal-hook-registry" 257 | version = "1.4.2" 258 | source = "registry+https://github.com/rust-lang/crates.io-index" 259 | checksum = "a9e9e0b4211b72e7b8b6e85c807d36c212bdb33ea8587f7569562a84df5465b1" 260 | dependencies = [ 261 | "libc", 262 | ] 263 | 264 | [[package]] 265 | name = "smallvec" 266 | version = "1.13.2" 267 | source = "registry+https://github.com/rust-lang/crates.io-index" 268 | checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" 269 | 270 | [[package]] 271 | name = "socket2" 272 | version = "0.5.7" 273 | source = "registry+https://github.com/rust-lang/crates.io-index" 274 | checksum = "ce305eb0b4296696835b71df73eb912e0f1ffd2556a501fcede6e0c50349191c" 275 | dependencies = [ 276 | "libc", 277 | "windows-sys", 278 | ] 279 | 280 | [[package]] 281 | name = "syn" 282 | version = "2.0.77" 283 | source = "registry+https://github.com/rust-lang/crates.io-index" 284 | checksum = "9f35bcdf61fd8e7be6caf75f429fdca8beb3ed76584befb503b1569faee373ed" 285 | dependencies = [ 286 | "proc-macro2", 287 | "quote", 288 | "unicode-ident", 289 | ] 290 | 291 | [[package]] 292 | name = "tokio" 293 | version = "1.42.0" 294 | source = "registry+https://github.com/rust-lang/crates.io-index" 295 | checksum = "5cec9b21b0450273377fc97bd4c33a8acffc8c996c987a7c5b319a0083707551" 296 | dependencies = [ 297 | "backtrace", 298 | "bytes", 299 | "libc", 300 | "mio", 301 | "parking_lot", 302 | "pin-project-lite", 303 | "signal-hook-registry", 304 | "socket2", 305 | "tokio-macros", 306 | "windows-sys", 307 | ] 308 | 309 | [[package]] 310 | name = "tokio-macros" 311 | version = "2.4.0" 312 | source = "registry+https://github.com/rust-lang/crates.io-index" 313 | checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752" 314 | dependencies = [ 315 | "proc-macro2", 316 | "quote", 317 | "syn", 318 | ] 319 | 320 | [[package]] 321 | name = "unicode-ident" 322 | version = "1.0.13" 323 | source = "registry+https://github.com/rust-lang/crates.io-index" 324 | checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" 325 | 326 | [[package]] 327 | name = "wasi" 328 | version = "0.11.0+wasi-snapshot-preview1" 329 | source = "registry+https://github.com/rust-lang/crates.io-index" 330 | checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" 331 | 332 | [[package]] 333 | name = "windows-sys" 334 | version = "0.52.0" 335 | source = "registry+https://github.com/rust-lang/crates.io-index" 336 | checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" 337 | dependencies = [ 338 | "windows-targets", 339 | ] 340 | 341 | [[package]] 342 | name = "windows-targets" 343 | version = "0.52.6" 344 | source = "registry+https://github.com/rust-lang/crates.io-index" 345 | checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" 346 | dependencies = [ 347 | "windows_aarch64_gnullvm", 348 | "windows_aarch64_msvc", 349 | "windows_i686_gnu", 350 | "windows_i686_gnullvm", 351 | "windows_i686_msvc", 352 | "windows_x86_64_gnu", 353 | "windows_x86_64_gnullvm", 354 | "windows_x86_64_msvc", 355 | ] 356 | 357 | [[package]] 358 | name = "windows_aarch64_gnullvm" 359 | version = "0.52.6" 360 | source = "registry+https://github.com/rust-lang/crates.io-index" 361 | checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" 362 | 363 | [[package]] 364 | name = "windows_aarch64_msvc" 365 | version = "0.52.6" 366 | source = "registry+https://github.com/rust-lang/crates.io-index" 367 | checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" 368 | 369 | [[package]] 370 | name = "windows_i686_gnu" 371 | version = "0.52.6" 372 | source = "registry+https://github.com/rust-lang/crates.io-index" 373 | checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" 374 | 375 | [[package]] 376 | name = "windows_i686_gnullvm" 377 | version = "0.52.6" 378 | source = "registry+https://github.com/rust-lang/crates.io-index" 379 | checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" 380 | 381 | [[package]] 382 | name = "windows_i686_msvc" 383 | version = "0.52.6" 384 | source = "registry+https://github.com/rust-lang/crates.io-index" 385 | checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" 386 | 387 | [[package]] 388 | name = "windows_x86_64_gnu" 389 | version = "0.52.6" 390 | source = "registry+https://github.com/rust-lang/crates.io-index" 391 | checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" 392 | 393 | [[package]] 394 | name = "windows_x86_64_gnullvm" 395 | version = "0.52.6" 396 | source = "registry+https://github.com/rust-lang/crates.io-index" 397 | checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" 398 | 399 | [[package]] 400 | name = "windows_x86_64_msvc" 401 | version = "0.52.6" 402 | source = "registry+https://github.com/rust-lang/crates.io-index" 403 | checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" 404 | 405 | [[package]] 406 | name = "zerocopy" 407 | version = "0.7.35" 408 | source = "registry+https://github.com/rust-lang/crates.io-index" 409 | checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" 410 | dependencies = [ 411 | "byteorder", 412 | "zerocopy-derive", 413 | ] 414 | 415 | [[package]] 416 | name = "zerocopy-derive" 417 | version = "0.7.35" 418 | source = "registry+https://github.com/rust-lang/crates.io-index" 419 | checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" 420 | dependencies = [ 421 | "proc-macro2", 422 | "quote", 423 | "syn", 424 | ] 425 | --------------------------------------------------------------------------------