Git Product home page Git Product logo

guessing_game's Introduction

If you'd like to learn Rust, The Book from Rust's official website, is freely available and quite good.

I've gotten to chapter 10ish and have adapted the book's guessing game to flex my newfound skills. Here's that game.

secret_numbers.rs defines the SecretNumber struct, representing a guessable secret number and Accuracy, representing how arbitrarily close to the guess a given number is.

Some interesting points are the derived traits automatically provided by the compiler by decorating the Accuracy enum with #[derive(PartialEq, Eq, Hash)].

  • PartialEq and Eq implements functionality to compare values and, well - if you don't have it (or your own implementation), the compuler will tell you

error[E0277]: can't compare secret_number::Accuracy with secret_number::Accuracy --> src\secret_number.rs:12:10 | 12 | #[derive(Eq, Hash, Debug)] | ^^ no implementation for secret_number::Accuracy == secret_number::Accuracy | = help: the trait std::cmp::PartialEq is not implemented for secret_number::Accuracy

  • Hash implements the supporting functionality to put values in a HashMap (a Dictionary).

main.rs is the game itself, providing IO functionality and some randomness to make the game more interesting.

  • prompt asks the user a question, ensuring that the answer is between a given range of values.
  • random_prompt_string generates a semi-random question to ask.
  • main is the game loop.

guessing_game's People

Contributors

samilamti 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.