# Homework 2: From Infix to Postfix Expressions Submiesion instructions cargo ne
ID: 3603532 • Letter: #
Question
# Homework 2: From Infix to Postfix Expressions Submiesion instructions cargo new 2 name your ed 2 Save your program in src/lib.rs git add Cargo.toml erc/lib.re git commit git remote add origin metastasis@gradebot.org:user/username)1/2 git pueh origin master Because postfix expreesione are simpler and faster to evaluate, compilere transform infix expressions in source programe to postfix expressions in executable programs. You will implement this transformation ** Infix expressions An infix expression contains one or more of the following tokens: * operands; integers, such as 1,-23°. * Operators: arithmetic operators-+ , , and with their normal association and Left and right parentheses A valid infix expression must satisfy all the following rules: *An operand or left parentheaia cannot be preceded by an operand or right parentheeis An operator or right parenthesis annot *be the firat token in the expression, or be preceded by an operator or left parentheais *An operator or left parentheie cannot be the last token in the expreasion Each left parenthesis must match a unique right parenthesis, and vice versa Tranaform infix to postfix e expresa1on 1. Create a etack 2. Scan the tokens in the infix expression If the token is an operand, output it If the token is a left parenthesie, push it onto the etac!k If the token ia a right parentheais, pop and output all the operators from the stack until encountering a left parenthesis. Pop and diacard the left parenthesis If the token is an operator If the tack is empty, push the token onto the tack otherwise, if the top of the stack is an operator and its precedence ia greater than or egual to the precedence of the token, pop and output the operator from the atack, and repeat this process. Finall y, push the token in the in Eix expression onto the stackExplanation / Answer
A tool for formatting Rust code according to style guidelines.
If you'd like to help out (and you should, it's a fun project!), see Contributing.md.
We are changing the default style used by rustfmt. There is an ongoing RFC process. The last version using the old style was 0.8.6. From 0.9 onwards, the RFC style is the default. If you want the old style back, you can use legacy-rustfmt.toml as your rustfmt.toml.
The current master branch uses libsyntax (part of the compiler). It is published as rustfmt-nightly. The syntex branch uses Syntex instead of libsyntax, it is published (for now) as rustfmt. Most development happens on the master branch, however, this only supports nightly toolchains. If you use stable or beta Rust toolchains, you must use the Syntex version (which is likely to be a bit out of date). Version 0.1 of rustfmt-nightly is forked from version 0.9 of the syntex branch.
Quick start
You must be using the latest nightly compiler toolchain.
To install:
to run on a cargo project in the current working directory:
Installation
or if you're using Rustup
If you don't have a nightly toolchain, you can add it using rustup:
You can make the nightly toolchain the default by running:
If you choose not to do that you'll have to run rustfmt using rustup run ... or by adding +nightly to the cargo invocation.
Usually cargo-fmt, which enables usage of Cargo subcommand cargo fmt, is installed alongside rustfmt. To only install rustfmt run
Installing from source
To install from source, first checkout to the tag or branch you want to install, then issue
This will install rustfmt in your ~/.cargo/bin. Make sure to add ~/.cargo/bin directory to your PATH variable.
Running
You can run Rustfmt by just typing rustfmt filename if you used cargo install. This runs rustfmt on the given file, if the file includes out of line modules, then we reformat those too. So to run on a whole module or crate, you just need to run on the root file (usually mod.rs or lib.rs). Rustfmt can also read data from stdin. Alternatively, you can use cargo fmt to format all binary and library targets of your crate.
You'll probably want to specify the write mode. Currently, there are modes for diff, replace, overwrite, display, coverage, checkstyle, and plain.
The write mode can be set by passing the --write-mode flag on the command line. For example rustfmt --write-mode=display src/filename.rs
cargo fmt uses --write-mode=overwrite by default.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.