working code
This commit is contained in:
parent
09ffbede24
commit
c5dd891290
6
Cargo.lock
generated
Normal file
6
Cargo.lock
generated
Normal file
@ -0,0 +1,6 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "wasi-tutorial"
|
||||
version = "0.1.0"
|
||||
|
||||
1
output.txt
Normal file
1
output.txt
Normal file
@ -0,0 +1 @@
|
||||
Hello
|
||||
33
src/main.rs
33
src/main.rs
@ -1,3 +1,32 @@
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
use std::env;
|
||||
use std::fs;
|
||||
use std::io::{Read, Write};
|
||||
|
||||
fn process(input_fname: &str, output_fname: &str) -> Result<(), String> {
|
||||
let mut input_file =
|
||||
fs::File::open(input_fname).map_err(|err| format!("error opening input: {}", err))?;
|
||||
let mut contents = Vec::new();
|
||||
input_file
|
||||
.read_to_end(&mut contents)
|
||||
.map_err(|err| format!("read error: {}", err))?;
|
||||
|
||||
let mut output_file = fs::File::create(output_fname)
|
||||
.map_err(|err| format!("error opening output '{}': {}", output_fname, err))?;
|
||||
output_file
|
||||
.write_all(&contents)
|
||||
.map_err(|err| format!("write error: {}", err))
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let args: Vec<String> = env::args().collect();
|
||||
let program = args[0].clone();
|
||||
|
||||
if args.len() < 3 {
|
||||
eprintln!("{} <input_file> <output_file>", program);
|
||||
return;
|
||||
}
|
||||
|
||||
if let Err(err) = process(&args[1], &args[2]) {
|
||||
eprintln!("{}", err)
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user