Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PROGRAM IN RUST Your program will print a Rust Vector that you create by continually pushing tokens onto it. This assignment is intended to give

PROGRAM IN RUST

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

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: o One (doesn't store any data) O ACouple (doesn't store any data) AFew (doesn't store any data) o Several (doesn't store any data) Amount (stores a u32) Create an enum called Token with the following variants: o Amount (stores a Quantity, the previous enum that you create) Character (stores a char) o Whitespace (doesn't store any data) Write a program function that does the following: o 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 o Creates a Token object each iterations based on the current character o Pushes the Token onto tokens o Prints tokens 4 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> {}) 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. If you run into any problems, please ask for help on Slack. ts print_tokens. rs test1.txt UTCDTLV Muu 1 cu > rustc print_tokens.rs > /print_tokens test.txt [Character('H'), Character('e'), Character('l'), Character('l'), Character('o'), Whitespace, Amount (One), Character(','), Whitespace, Amount (Acouple), Character (','), Whitespace, Amount (AFew), Character(','), Whitespace, Amount (Several), Character(','), Whitespace, Amount (Several), Character(','), Whitespace, Amount (Se veral), Character(','), Whitespace, Amount (Several), Character(','), Whitespace, Amount (Other(8)), Character(','), Whitespace, Amount(Other(9)), Character(','), Whitespace, Character('a'), Character('n'), Character('d'), Whitespace, Character('t'), Character('e'), Character('n'), Character('.'), Whitespace] 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: o One (doesn't store any data) O ACouple (doesn't store any data) AFew (doesn't store any data) o Several (doesn't store any data) Amount (stores a u32) Create an enum called Token with the following variants: o Amount (stores a Quantity, the previous enum that you create) Character (stores a char) o Whitespace (doesn't store any data) Write a program function that does the following: o 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 o Creates a Token object each iterations based on the current character o Pushes the Token onto tokens o Prints tokens 4 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> {}) 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. If you run into any problems, please ask for help on Slack. ts print_tokens. rs test1.txt UTCDTLV Muu 1 cu > rustc print_tokens.rs > /print_tokens test.txt [Character('H'), Character('e'), Character('l'), Character('l'), Character('o'), Whitespace, Amount (One), Character(','), Whitespace, Amount (Acouple), Character (','), Whitespace, Amount (AFew), Character(','), Whitespace, Amount (Several), Character(','), Whitespace, Amount (Several), Character(','), Whitespace, Amount (Se veral), Character(','), Whitespace, Amount (Several), Character(','), Whitespace, Amount (Other(8)), Character(','), Whitespace, Amount(Other(9)), Character(','), Whitespace, Character('a'), Character('n'), Character('d'), Whitespace, Character('t'), Character('e'), Character('n'), Character('.'), Whitespace]

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Expert Oracle Database Architecture

Authors: Thomas Kyte, Darl Kuhn

3rd Edition

1430262990, 9781430262992

More Books

Students also viewed these Databases questions