Git Product home page Git Product logo

guessing-game's Introduction

Rust Guessing Game

Welcome to the Rust Guessing Game! This project is a hands-on introduction to Rust, covering fundamental concepts such as variables, user input handling, external crates, and control flow. The guessing game is a classic programming exercise, and building it in Rust offers a unique opportunity to explore the language's features and idioms.

About the Game

The guessing game challenges the player to guess a random number generated by the program. Here's how it works:

  • The program generates a random integer between 1 and 100.
  • It prompts the player to enter a guess.
  • Upon receiving input, the program evaluates the guess:
    • If the guess is too low, it prompts the player to guess higher.
    • If the guess is too high, it suggests guessing lower.
    • If the guess is correct, the program congratulates the player and exits.

This simple game introduces several Rust concepts, including let bindings, the match control flow operator, methods, associated functions, and the use of external crates (in this case, for generating random numbers).

Getting Started

To play the guessing game, you'll need to have Rust installed on your machine. If you haven't already, you can install Rust by following the instructions at https://www.rust-lang.org/tools/install.

Once Rust is installed, follow these steps to download and run the game:

  1. Clone the repository:

    git clone https://github.com/yourusername/rust-guessing-game.git
    
  2. Change into the project directory:

cd rust-guessing-game
  1. Compile and run the game:
cargo run

Concepts Covered:

  • Standard Input and Output (I/O): Demonstrates basic I/O operations in Rust using std::io, including reading from stdin and writing to stdout. It highlights the importance of handling user input in interactive applications.

  • Mutable Variables: Shows how to declare mutable variables in Rust with let mut, allowing their values to be changed after initialization, which is essential for capturing and updating user input.

  • String Handling: Introduces string creation and manipulation, emphasizing the dynamic, growable nature of String objects in Rust and their ability to be modified.

  • Type Conversion and Error Handling: Utilizes Rust's powerful pattern matching with match expressions to safely convert strings to integers and handle potential errors gracefully, showcasing Rust's approach to error handling that favors explicitness and safety.

  • Loops: Utilizes loop, an infinite loop construct, to continuously prompt the user for input until a specific condition is met (i.e., the correct guess), demonstrating control flow mechanisms in Rust.

  • Random Number Generation: Leverages the rand crate to generate random numbers, illustrating how to use external crates and their functionalities, as well as the trait system in Rust through the use of the Rng trait for random number generation.

  • Enums and Pattern Matching: Introduces the use of the Ordering enum and pattern matching to compare numbers, highlighting Rust's enum and exhaustive pattern matching capabilities for writing clear and concise decision-making logic.

  • Break Statement: Shows the use of break to exit loops, a common control flow mechanism that allows terminating loop execution based on a condition, in this case, guessing the correct number.

guessing-game's People

Contributors

juanc004 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.