Exercism v3 launches on Sept 1st 2021. Learn more! ๐Ÿš€๐Ÿš€๐Ÿš€
Avatar of PatrickMcSweeny

PatrickMcSweeny's solution

to Grains in the Rust Track

Published at Jul 19 2019 · 0 comments
Test suite


This exercise has changed since this solution was written.

Calculate the number of grains of wheat on a chessboard given that the number on each square doubles.

There once was a wise servant who saved the life of a prince. The king promised to pay whatever the servant could dream up. Knowing that the king loved chess, the servant told the king he would like to have grains of wheat. One grain on the first square of a chess board, with the number of grains doubling on each successive square.

There are 64 squares on a chessboard (where square 1 has one grain, square 2 has two grains, and so on).

Write code that shows:

  • how many grains were on a given square, and
  • the total number of grains on the chessboard

For bonus points

Did you get the tests passing and the code clean? If you want to, these are some additional things you could try:

  • Optimize for speed.
  • Optimize for readability.

Then please share your thoughts in a comment on the submission. Did this experiment make the code better? Worse? Did you learn anything from it?

Rust Installation

Refer to the exercism help page for Rust installation and learning resources.

Writing the Code

Execute the tests with:

$ cargo test

All but the first test have been ignored. After you get the first test to pass, open the tests source file which is located in the tests directory and remove the #[ignore] flag from the next test and get the tests to pass again. Each separate test is a function with #[test] flag above it. Continue, until you pass every test.

If you wish to run all tests without editing the tests source file, use:

$ cargo test -- --ignored

To run a specific test, for example some_test, you can use:

$ cargo test some_test

If the specific test is ignored use:

$ cargo test some_test -- --ignored

To learn more about Rust tests refer to the online test documentation

Make sure to read the Modules chapter if you haven't already, it will help you with organizing your files.

Further improvements

After you have solved the exercise, please consider using the additional utilities, described in the installation guide, to further refine your final solution.

To format your solution, inside the solution directory use

cargo fmt

To see, if your solution contains some common ineffective use cases, inside the solution directory use

cargo clippy --all-targets

Submitting the solution

Generally you should submit all files in which you implemented your solution (src/lib.rs in most cases). If you are using any external crates, please consider submitting the Cargo.toml file. This will make the review process faster and clearer.

Feedback, Issues, Pull Requests

The exercism/rust repository on GitHub is the home for all of the Rust exercises. If you have feedback about an exercise, or want to help implement new exercises, head over there and create an issue. Members of the rust track team are happy to help!

If you want to know more about Exercism, take a look at the contribution guide.


JavaRanch Cattle Drive, exercise 6 http://www.javaranch.com/grains.jsp

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.


use grains;

fn process_square_case(input: u32, expected: u64) {
    assert_eq!(grains::square(input), expected);

/// 1
fn test_1() {
    process_square_case(1, 1);

/// 2
fn test_2() {
    process_square_case(2, 2);

/// 3
fn test_3() {
    process_square_case(3, 4);

/// 4
fn test_4() {
    process_square_case(4, 8);

/// 16
fn test_16() {
    process_square_case(16, 32_768);

/// 32
fn test_32() {
    process_square_case(32, 2_147_483_648);

/// 64
fn test_64() {
    process_square_case(64, 9_223_372_036_854_775_808);

#[should_panic(expected = "Square must be between 1 and 64")]
fn test_square_0_raises_an_exception() {

#[should_panic(expected = "Square must be between 1 and 64")]
fn test_square_greater_than_64_raises_an_exception() {

fn test_returns_the_total_number_of_grains_on_the_board() {
    assert_eq!(grains::total(), 18_446_744_073_709_551_615);
const SQUARES: u32 = 64;

pub fn square(s: u32) -> u64 {
    if s < 1 || s > SQUARES {
        panic!("Square must be between 1 and {}", SQUARES);
    2_u64.pow(s - 1)

pub fn total() -> u64 {
    (1..=SQUARES).map(|number| square(number)).sum()

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other peopleโ€™s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?