Rust Challenge: Balanced Parentheses Checker
Learning Rust Through Small Challenges

Hello, Rustaceans! It’s time for another Rust challenge. When learning something new, it’s essential to start with the basics to help you gain understand of how the language/tool works under the hood. One of the best ways to do this is by putting the compiler to work and exploring its capabilities.
I’m also in the process of learning Rust, and I believe that these small challenges are valuable for gaining a deeper understanding of the language and becoming more comfortable with it.
So, roll up your sleeves and take on this challenge.
Challenge: Parentheses Checker
Write a function that takes a string containing a sequence of parentheses, brackets, and curly braces (e.g., “([]{})”) and checks if the brackets are balanced and properly nested. The function should return true if the input is balanced and false if it’s not.
For example, the function should return true for “([]{})” but false for “([)]” because the brackets in the latter are not properly nested.
This challenge will test your knowledge of stack data structures and string manipulation in Rust.
Happy coding!
Thoughts on this challenge
As the challenge suggests, it aims to evaluate our understanding of data structures in Rust, particularly stacks, and various string manipulation techniques like iterations. My approach to this challenge is as follows:
We can iterate through the characters of the provided string and store each character in a stack. During each iteration, we check if the current character is already present in our stack and return the corresponding boolean value based on its availability.
Since Rust doesn’t have built-in stack data structures, we can utilize Rust’s Vectors to effectively address this challenge.
Solution:
I am aware that this code may not adhere to idiomatic Rust conventions and can be enhanced in several aspects.
use std::io::stdin;
use std::collections::HashMap;
fn main() {
println!("Parentheses Checker");
let mut user_text = String::new();
stdin().read_line(&mut user_text).expect("Something went wrong");
let is_balanced = is_parentheses(&user_text);
if is_balanced {
println!("The parantheses in {} is balanced", user_text);
}else{
println!("The parantheses in {} are not balanced", user_text);
}
}
fn is_parentheses(text: &str)-> bool {
let mut stack: Vec<char>= Vec::new();
for symbol in text.chars(){
match symbol {
'(' | '[' | '{' => stack.push(symbol),
')' =>{
if stack.pop() != Some('('){
return false
}
},
']' =>{
if stack.pop() != Some('['){
return false
}
}
'}' =>{
if stack.pop() != Some('{'){
return false
}
}
_=>{}
}
}
stack.is_empty()
}You can run the code on Rust Playground.
If you notice various areas for improvement or edge cases to cover, I would love to hear from you. If you have alternative ways to solve this challenge, it would be helpful to learn from your solution.
Before you go
I really appreciate you reading this article this far. If you love Rust and would love to share your knowledge, you are highly welcomed to join the publication. Check out Writing for Rustaceans.
You can follow the publication to get more challenges like this in your feed.
Happy coding, Rustaceans!





