Skip to content

Instantly share code, notes, and snippets.

@bynect
Last active December 18, 2020 16:52
Show Gist options
  • Save bynect/2c13c238f29c9d8d2fac4f5e25fcf5ed to your computer and use it in GitHub Desktop.
Save bynect/2c13c238f29c9d8d2fac4f5e25fcf5ed to your computer and use it in GitHub Desktop.
Educational implementation of the echo command in Rust.
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