avatarJohn Philip

Summary

The undefined website presents a Rust programming challenge to create a balanced parentheses checker, offering insights into Rust's data structures and string manipulation, and invites contributions to the Rustacean publication on Medium.

Abstract

The website content introduces a Rust challenge aimed at assessing the reader's understanding of stack data structures and string manipulation. Participants are tasked with writing a function that verifies whether a string of parentheses, brackets, and curly braces is balanced and properly nested. The article provides a detailed explanation of the challenge, a proposed solution using Rust's Vector as a stack, and encourages readers to improve upon the code or offer alternative solutions. Additionally, the author expresses their own learning journey with Rust and emphasizes the value of such challenges in deepening one's knowledge of the language. The article also includes a call to action for Rust enthusiasts to join the Rustacean publication on Medium and contribute their expertise.

Opinions

  • The author believes that small coding challenges are crucial for gaining a deeper understanding of a programming language like Rust.
  • They suggest that iterating through the string and using a stack to check for balanced parentheses is an effective approach.
  • The author acknowledges that the provided solution may not be idiomatic Rust and welcomes feedback for improvement.
  • There is an open invitation for Rust developers to share their knowledge by writing for the Rustacean publication, indicating a community-driven approach to learning and sharing knowledge.
  • The author expresses enthusiasm for Rust's capabilities and the use of Vectors to simulate stack behavior, showcasing Rust's flexibility in data structure implementation.

Rust Challenge: Balanced Parentheses Checker

Learning Rust Through Small Challenges

Rust Mascot Ferris

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!

More reads

Rust
Rustlang
Rust Programming Language
Programming Languages
Programming
Recommended from ReadMedium