v0.1.0: some args handling
This commit is contained in:
		
							parent
							
								
									67f288c561
								
							
						
					
					
						commit
						fa301ca9f1
					
				
							
								
								
									
										4
									
								
								Cargo.lock
									
									
									
										generated
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								Cargo.lock
									
									
									
										generated
									
									
									
										Normal file
									
								
							| @ -0,0 +1,4 @@ | ||||
| [[package]] | ||||
| name = "grep" | ||||
| version = "0.1.0" | ||||
| 
 | ||||
| @ -31,4 +31,10 @@ History | ||||
| 
 | ||||
| ```bash | ||||
| cargo init --bin --name grep | ||||
| cargo run | ||||
| 
 | ||||
| git clone --depth=1 https://github.com/rust-lang/rust.vim.git ~/.vim/bundle/rust.vim | ||||
| git clone --depth=1 https://github.com/majutsushi/tagbar.git ~/.vim/bundle/tagbar | ||||
| echo 'let g:tagbar_ctags_bin = "/Users/aj/github.com/universal-ctags/ctags/ctags"' >> ~/.vimrc | ||||
| echo 'let g:rustfmt_autosave = 1' >> ~/.vimrc | ||||
| ``` | ||||
|  | ||||
							
								
								
									
										9
									
								
								poem.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								poem.txt
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,9 @@ | ||||
| I'm nobody! Who are you? | ||||
| Are you nobody, too? | ||||
| Then there's a pair of us - don't tell! | ||||
| Theyd' banish us, you know. | ||||
| 
 | ||||
| How dreary to be somebody! | ||||
| How public, like a frog | ||||
| To tell your name the livelong day | ||||
| To an admiring bog! | ||||
							
								
								
									
										3
									
								
								src/lib.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								src/lib.rs
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,3 @@ | ||||
| pub fn run() { | ||||
|     println!("hello") | ||||
| } | ||||
							
								
								
									
										39
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										39
									
								
								src/main.rs
									
									
									
									
									
								
							| @ -1,3 +1,38 @@ | ||||
| fn main() { | ||||
|     println!("Hello, world!"); | ||||
| extern crate grep; | ||||
| 
 | ||||
| use std::env; | ||||
| use std::fs::File; | ||||
| use std::io::prelude::*; | ||||
| 
 | ||||
| struct Config { | ||||
|     query: String, | ||||
|     filename: String, | ||||
| } | ||||
| 
 | ||||
| fn main() { | ||||
|     let args: Vec<String> = env::args().collect(); | ||||
| 
 | ||||
|     let conf = parse_config(&args); | ||||
| 
 | ||||
|     println!("{:?} {:?}", conf.query, conf.filename); | ||||
| 
 | ||||
|     let mut f = File::open(&conf.filename).expect(&("file not found: ".to_owned() + &conf.filename)); | ||||
|     //let mut f = File::open(filename).expect(&format!("file not found: {}", &filename));
 | ||||
| 
 | ||||
|     let mut contents = String::new(); | ||||
|     f.read_to_string(&mut contents) | ||||
|         .expect("something went wrong reading the file"); | ||||
| 
 | ||||
|     println!("Searching for '{}'", conf.query); | ||||
|     println!("In file '{}'", conf.filename); | ||||
|     println!("With text:\n{}", contents); | ||||
| } | ||||
| 
 | ||||
| //fn parse_config(args: &[String]) -> (&String, &String)
 | ||||
| //fn parse_config(args: &[String]) -> (&str, &str)
 | ||||
| fn parse_config(args: &[String]) -> Config { | ||||
|     Config { | ||||
|         query: args[1].clone(), | ||||
|         filename: args[2].clone(), | ||||
|     } | ||||
| } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user