-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrust_programming_language_basic_template.rs
More file actions
41 lines (36 loc) · 1.22 KB
/
rust_programming_language_basic_template.rs
File metadata and controls
41 lines (36 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
use std::env;
use std::fs::File;
use std::io::{BufRead, BufReader};
//compile with: rustc rust_programming_language_basic_template.rs
//run with: ./rust_programming_language_basic_template test.test
fn build_codex(codex: &mut Vec<String>, name: &str) {
//open file from input name
let script = File::open(name).expect("Failed to open file");
//collect all lines from file and place in vector codex
let reader = BufReader::new(script);
for line in reader.lines() {
codex.push(line.expect("Failed to read line"));
}
}
fn read_codex(codex: &Vec<String>) {
let length = codex.len();
//read instructions inside codex and interpret results
for i in 0..length {
if codex[i] == "print" {
//if print found increment line by one and print next line
let next_line = i + 1;
println!("{}", codex[next_line]);
}
}
}
fn main() {
//vector to hold instruction
let mut codex: Vec<String> = Vec::new();
//make sure there is a script listed in the command line argument
if let Some(name) = env::args().nth(1) {
//import script
build_codex(&mut codex, &name);
//interpret instructions
read_codex(&codex);
}
}