Last active
September 4, 2023 20:17
-
-
Save pcgeek86/13df1702848bfbd82b47af35afc67efb to your computer and use it in GitHub Desktop.
Rust Cheatsheet Examples 🦀
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This example shows how to prompt a user for input | |
// and convert the results to a Rust String type. | |
fn main() { | |
let buffer = &mut String::from(""); | |
std::io::stdin().read_line(buffer).unwrap(); | |
let input = buffer.replace("\n", ""); | |
println!("{}no-new-line", input); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 🦀 Example of spawning thread and | |
// waiting for it to finish its work. | |
use std::thread::spawn; | |
fn main() { | |
let work = || println!("Doing thread work"); // Define a Rust closure that will perform some work in a thread | |
let handle = spawn(work); // spawn() function kicks off new thread, using closure | |
_ = handle.join(); // .join() waits for thread to finish | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment