rust language The program argument is a filename. Your program will read the fil
ID: 3884081 • Letter: R
Question
rust language
The program argument is a filename. Your program will read the file and print all of the tokens that it finds. Specifically, your program should be able to detect the following tokens: + rightarrow Plus - rightarrow Minus * rightarrow Stan /rightarrow Slash = rightarrow Equal (rightarrow LeftParen) rightarrow RightParen: rightarrow Semicolon rightarrow Comma For example, with the following input file: +-*/ Your program should produce the following output: Plus Minus Stan Slash Note, your program should skip any whitespace characters that you see (see is_whitespace), and it should stop if it encounters an unknown character. For example, with the following input file (note the spaces between each symbol): + - @ */ Your program should output: Plus MinusExplanation / Answer
use std::env;
use std::fs::File;
use std::io::prelude::*;
fn main(){
let args: Vec<String> = env::args().collect();
let file = &args[1];
let mut f = File::open(file).expect("file not found");
let mut contents = String::new();
f.read_to_string(&mut contents)
.expect("something went wrong reading the file");
for c in contents.chars() {
if c=='+'
{
println!("Plus");
}
else if c=='*'
{
println!("Star");
}
else if c=='-'
{
println!("Minus");
}
else if c=='/'
{
println!("Slash");
}
else if c=='='
{
println!("Equals");
}
else if c=='('
{
println!("LeftParen");
}
else if c==')'
{
println!("RightParen");
}
else if c==';'
{
println!("SemiColon");
}
else if c==','
{
println!("Comma");
}
else if c==' '
{
//donothing
}
else
{
break;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.