Last active
December 18, 2020 16:52
-
-
Save bynect/2c13c238f29c9d8d2fac4f5e25fcf5ed to your computer and use it in GitHub Desktop.
Educational implementation of the echo command in Rust.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pub fn main() { | |
// collect the command line arguments into a vector of string | |
let argv: Vec<String> = std::env::args().collect(); | |
// iterate over the vector of args ignoring the first argument | |
for arg in &argv[1..] { | |
// ignore args starting with '-' | |
if !arg.starts_with("-") { | |
// print argument without newline | |
print!( | |
"{} ", | |
arg | |
// replace escape sequences with their hex code | |
.replace("\\b", "\x08") | |
.replace("\\t", "\x09") | |
.replace("\\n", "\x0a") | |
.replace("\\v", "\x0b") | |
.replace("\\f", "\x0c") | |
.replace("\\r", "\x0d") | |
); | |
} | |
} | |
// print a trailing newline | |
println!(""); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment