learning rust do not look

init

besaid.zone 2f37125e

+1
.gitignore
···
··· 1 + /target
+7
Cargo.lock
···
··· 1 + # This file is automatically @generated by Cargo. 2 + # It is not intended for manual editing. 3 + version = 4 4 + 5 + [[package]] 6 + name = "yap" 7 + version = "0.1.0"
+6
Cargo.toml
···
··· 1 + [package] 2 + name = "yap" 3 + version = "0.1.0" 4 + edition = "2024" 5 + 6 + [dependencies]
+1
simple.txt
···
··· 1 + Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
+47
src/main.rs
···
··· 1 + use std::{env, fs}; 2 + 3 + struct Config { 4 + file_path: String, 5 + command: String 6 + } 7 + 8 + impl Config { 9 + fn new(args: &[String]) -> Config { 10 + let path = args[2].clone(); 11 + let command = args[1].clone(); 12 + 13 + Config { file_path: path, command } 14 + } 15 + } 16 + 17 + fn main() { 18 + let args: Vec<String> = env::args().collect(); 19 + 20 + let config = Config::new(&args); 21 + 22 + 23 + let file_contents = fs::read_to_string(config.file_path).expect("Invalid file path, please make sure the path to the file is correct"); 24 + // println!("file contents length with spaces: {}", file_contents.chars().count()); 25 + dbg!(count_words(file_contents)); 26 + 27 + match config.command.as_str() { 28 + "-c" | "--bytes"=> println!("bytes"), 29 + _ => println!("Unknown command, see --help") 30 + } 31 + } 32 + 33 + fn count_bytes() { 34 + todo!("not impl") 35 + } 36 + 37 + fn count_chars() { 38 + todo!("not impl") 39 + } 40 + 41 + fn count_lines(contents: String) { 42 + println!("{}", contents.lines().count()) 43 + } 44 + 45 + fn count_words(contents: String) { 46 + print!("{}", contents.split_whitespace().count()) 47 + }