Question: Programing in Rust. Your program will print a Rust Vector that you create by continually pushing tokens onto it. This assignment is intended to give

Programing in Rust.

Your program will print a Rust Vector that you create by continually pushing tokens onto it. This assignment is intended to give you practice with the following concepts:

  • enums
  • Vectors
  • pattern matching and match expressions

For this program, you will:

  • Create an enum called Quantity with the following variants:
    • One (doesn't store any data)
    • ACouple (doesn't store any data)
    • AFew (doesn't store any data)
    • Several (doesn't store any data)
    • Other (stores a u32)
  • Create an enum called Token with the following variants:
    • Amount (stores a Quantity, the previous enum that you create)
    • Character (stores a char)
    • Whitespace (doesn't store any data)
  • Write a program function that does the following:
    • Accepts one program argument that is the name of a file
    • Reads the contents of the given file into a string
    • Creates a Vec called tokens
    • Loops through the string character by character
    • Creates a Token object each iterations based on the current character
    • Pushes the Token onto tokens
    • Prints tokens

Writing Your Program

You will want to put #[derive(Debug)] above your two enums (Quantity and Token).

If you want to use the ? operator in your main function, then you will need to:

  • use the appropriate library (use std::error::Error;)
  • write the appropriate function signature for main (fn main() -> Result<(), Box> {})
  • return Ok(()) at the end of main

You will want to iterate through the contents of the file using a string iterator (call chars() on the String).

You will want to match on the current character. See the documentation for pattern syntax(https://doc.rust-lang.org/book/ch18-03-pattern-syntax.html).

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!