Rust practice match string
July 2, 2026, 1:19 p.m.
My general thoughts :
Learning a new programming language can be a challenging endeavor. I sometimes feel I might be hardwired for learning new languages coding or otherwise or maybe one lends itself to the other. Or maybe it might be my strategy of not going wide with languages but rather being selective with what I learn and go deep. That being said Id say the more challenging aspect of learning Rust was more about learning the syntax rather than the paradigm. You got loops and functions like some if not most of the languages but the character by character aspect or line buffer aspect is one I am new to. I could be completely mistaken in that other languages I know also have these features but it did feel more in control yet nitty gritty than for example python or lua.
What is incorporated :
In this code I incorporated a simple matching of a string using the match expression. I also incorporated in it a while loop as to keep the program running even if the user submits the incorrect string. Additionally, I added comments everywhere as to make it as simple and as human readable as I could. As I've learned life happens, you stop touching code for a while, and one can forget what was meant; so while writing comments may be time consuming I have a strong feeling its not time consuming at the end as getting back into coding it might be shorter time to get back into the grove of things from reading the comments.
What I learned :
I learned more about how to manage Rust programming. I learned it could be a bit of a struggle mostly because of its unfamiliar syntax, but on the flippity flip It did contain a lot of good error handling so that was really great in its favor, I feel without it I might have just looked elsewhere but since It sorta explained and gave tips I could hold on for longer than what I would have held on for had it not had the error messages.
use std::io; // crate::module
fn main() { // main logic is held here in this specific instance
println!(" Match the string to get out of the loop "); // title
println!(" \n by: taco_dev "); // print macro
println!(" \n Type \"Apple!\" : "); // \" = Literal; simple escape\
let mut input = String::new(); // Used to create new empty string ; with_capacity() pre-allocates space for 10 characters reducing the need for memory reallocation ; 1 of 2 ways to initialize a string; the other is " to_string() "
let mut x:bool = true; // mutable variable of type boolean set to truewhile x == true { // loop used as way to keep program running until correct string match returning false breaking the loop and therefore ending the program.
// Every time you enter an new iteration of the while loop,
// you should clear any items in the input buffer to ensure
// that the new requested input is not appended to the previous attempt.input.clear(); // clears line buffer elsewise it keeps looping as if incorrect if typed a mistake previously
match io::stdin().read_line(&mut input) { // locks handle and reads line of input appending to the specified buffer.
Ok(n) => {
println!("\n{n} bytes read");
println!("{input}");
}
Err(error) => println!("error: {error}"),
}match input.trim() { // .trim() used b/c probably contains hidden chars
"Apple!" => { // match arm when user types string then triggers code; setting variable to false breaking out of encapsulating while loop; return breaks out of match arm code logic (i think)
x = false;
println!("You did it!"); // ends loop
return;
}
_ => { // Wildcard match arm for user error since all options must be accounted for.
println!("failed : {}", input);
x = true; // keeps variable as true as to keep the while loop going to not crash/error out program
}
}}
}
Next Steps :
There is still more to learn (like with everything really). This was a small simple sample program to get the juices flowing to complete more difficult tasks. In the future I would like to expand this and maybe work on harder problems or make it more functional I'm still a bit unclear as It could also be a disposable program but if i ever get back to this specific program maybe I would like to try to make it into a package to invoke from the command line interface. Maybe I can also add quality of life features like a loading bar idk the sky is the limit. For now I think I will move on to other topics im unfamiliar with or weak at. Anywho, thanks for reading and keep it steady, peace!