Guest User

sort-TXT-RUST

a guest
Jul 8th, 2023
1,426
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 0.80 KB | None | 0 0
  1. use std::fs::File;
  2. use std::io::{Read, Write};
  3. use std::cmp::Reverse;
  4. use std::time::Instant;
  5.  
  6. fn sort_file(input_path: &str, output_path: &str) -> std::io::Result<()> {
  7.     let mut input_file = File::open(input_path)?;
  8.     let mut contents: Vec<u8> = Vec::new();
  9.     input_file.read_to_end(&mut contents)?;
  10.  
  11.     let space = ' ' as u8;
  12.     let space_count = contents.iter().filter(|&&c| c == space).count();
  13.     contents.retain(|&c| c != space);
  14.     contents.sort();
  15.     contents.extend(vec![space; space_count]);
  16.  
  17.     let mut output_file = File::create(output_path)?;
  18.     output_file.write_all(&contents)?;
  19.  
  20.     Ok(())
  21. }
  22.  
  23. fn main() {
  24.     let start = Instant::now();
  25.     sort_file("input.txt", "output.txt").unwrap();
  26.     let end = Instant::now();
  27.     println!("Rust Time {:?}", end - start);
  28. }
  29.  
Tags: rust
Advertisement
Add Comment
Please, Sign In to add comment