Add minigrep example program

This commit is contained in:
Timothy Warren 2019-01-31 16:20:46 -05:00
parent 9fec8ed7f9
commit a05a4eb790
7 changed files with 196 additions and 52 deletions

1
.idea/misc.xml generated
View File

@ -20,6 +20,7 @@
<cargoProject FILE="$PROJECT_DIR$/generics/Cargo.toml" />
<cargoProject FILE="$PROJECT_DIR$/lifetimes/Cargo.toml" />
<cargoProject FILE="$PROJECT_DIR$/adder/Cargo.toml" />
<cargoProject FILE="$PROJECT_DIR$/minigrep/Cargo.toml" />
</component>
<component name="ComposerJsonPluginSettings">
<unboundedVersionInspectionSettings>

5
.idea/rust.iml generated
View File

@ -78,6 +78,10 @@
<sourceFolder url="file://$MODULE_DIR$/adder/examples" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/adder/tests" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/adder/benches" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/minigrep/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/minigrep/examples" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/minigrep/tests" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/minigrep/benches" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/adder/target" />
<excludeFolder url="file://$MODULE_DIR$/branches/target" />
<excludeFolder url="file://$MODULE_DIR$/ctof/target" />
@ -89,6 +93,7 @@
<excludeFolder url="file://$MODULE_DIR$/guessing_game/target" />
<excludeFolder url="file://$MODULE_DIR$/lifetimes/target" />
<excludeFolder url="file://$MODULE_DIR$/loops/target" />
<excludeFolder url="file://$MODULE_DIR$/minigrep/target" />
<excludeFolder url="file://$MODULE_DIR$/modules/target" />
<excludeFolder url="file://$MODULE_DIR$/rectangles/target" />
<excludeFolder url="file://$MODULE_DIR$/references/target" />

105
.idea/workspace.xml generated
View File

@ -14,45 +14,29 @@
</component>
<component name="FileEditorManager">
<leaf SIDE_TABS_SIZE_LIMIT_KEY="300">
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/adder/src/lib.rs">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="1025">
<caret line="66" column="5" selection-start-line="66" selection-start-column="5" selection-end-line="66" selection-end-column="5" />
<folding>
<element signature="e#251#252#0" expanded="true" />
<element signature="e#263#264#0" expanded="true" />
<element signature="e#304#305#0" expanded="true" />
<element signature="e#338#339#0" expanded="true" />
<element signature="e#1036#1037#0" expanded="true" />
<element signature="e#1077#1078#0" expanded="true" />
<element signature="e#1119#1120#0" expanded="true" />
<element signature="e#1160#1161#0" expanded="true" />
<element signature="e#1196#1197#0" expanded="true" />
<element signature="e#1241#1242#0" expanded="true" />
<element signature="e#1191#1192#0" expanded="true" />
<element signature="e#1232#1233#0" expanded="true" />
<element signature="e#1520#1521#0" expanded="true" />
<element signature="e#1551#1552#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/adder/tests/integration_test.rs">
<entry file="file://$PROJECT_DIR$/minigrep/src/main.rs">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="96">
<caret line="6" column="20" selection-start-line="6" selection-start-column="20" selection-end-line="6" selection-end-column="20" />
<state relative-caret-position="320">
<caret line="20" lean-forward="true" selection-start-line="20" selection-end-line="20" />
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/adder/tests/common/mod.rs">
<entry file="file://$PROJECT_DIR$/minigrep/src/lib.rs">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="48">
<caret line="3" lean-forward="true" selection-start-line="3" selection-end-line="3" />
<state relative-caret-position="517">
<caret line="33" column="6" selection-start-line="33" selection-start-column="6" selection-end-line="33" selection-end-column="6" />
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/minigrep/poem.txt">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="128">
<caret line="8" column="19" selection-start-line="8" selection-start-column="19" selection-end-line="8" selection-end-column="19" />
</state>
</provider>
</entry>
@ -110,6 +94,9 @@
<option value="$PROJECT_DIR$/adder/src/lib.rs" />
<option value="$PROJECT_DIR$/adder/tests/common/mod.rs" />
<option value="$PROJECT_DIR$/adder/tests/integration_test.rs" />
<option value="$PROJECT_DIR$/minigrep/poem.txt" />
<option value="$PROJECT_DIR$/minigrep/src/lib.rs" />
<option value="$PROJECT_DIR$/minigrep/src/main.rs" />
</list>
</option>
</component>
@ -143,20 +130,13 @@
<path>
<item name="rust" type="3d21c010:ScopeViewTreeModel$ProjectNode" />
<item name="rust" type="2674bda8:ScopeViewTreeModel$GroupNode" />
<item name="adder" type="9f88c78c:ScopeViewTreeModel$FileNode" />
<item name="minigrep" type="9f88c78c:ScopeViewTreeModel$FileNode" />
</path>
<path>
<item name="rust" type="3d21c010:ScopeViewTreeModel$ProjectNode" />
<item name="rust" type="2674bda8:ScopeViewTreeModel$GroupNode" />
<item name="adder" type="9f88c78c:ScopeViewTreeModel$FileNode" />
<item name="tests" type="9f88c78c:ScopeViewTreeModel$FileNode" />
</path>
<path>
<item name="rust" type="3d21c010:ScopeViewTreeModel$ProjectNode" />
<item name="rust" type="2674bda8:ScopeViewTreeModel$GroupNode" />
<item name="adder" type="9f88c78c:ScopeViewTreeModel$FileNode" />
<item name="tests" type="9f88c78c:ScopeViewTreeModel$FileNode" />
<item name="common" type="9f88c78c:ScopeViewTreeModel$FileNode" />
<item name="minigrep" type="9f88c78c:ScopeViewTreeModel$FileNode" />
<item name="src" type="9f88c78c:ScopeViewTreeModel$FileNode" />
</path>
</expand>
<select />
@ -189,7 +169,7 @@
</component>
<component name="PropertiesComponent">
<property name="JavaScriptWeakerCompletionTypeGuess" value="true" />
<property name="com.android.tools.idea.instantapp.provision.ProvisionBeforeRunTaskProvider.myTimeStamp" value="1548965323798" />
<property name="com.android.tools.idea.instantapp.provision.ProvisionBeforeRunTaskProvider.myTimeStamp" value="1548969593161" />
<property name="javascript.nodejs.core.library.configured.version" value="7.1.0" />
<property name="js.eslint.eslintPackage" value="$USER_HOME$/.yarn-config/global/node_modules/.bin/eslint" />
<property name="js.eslint.nodeInterpreter" value="project" />
@ -327,8 +307,8 @@
<frame x="1680" y="-410" width="1050" height="1657" extended-state="6" />
<editor active="true" />
<layout>
<window_info active="true" content_ui="combo" id="Project" order="0" sideWeight="0.49663138" visible="true" weight="0.26141885" />
<window_info id="Structure" order="1" sideWeight="0.5033686" side_tool="true" visible="true" weight="0.26141885" />
<window_info content_ui="combo" id="Project" order="0" sideWeight="0.49710983" visible="true" weight="0.26141885" />
<window_info id="Structure" order="1" sideWeight="0.50289017" side_tool="true" visible="true" weight="0.26141885" />
<window_info id="Image Layers" order="2" />
<window_info id="Designer" order="3" />
<window_info id="Capture Tool" order="4" />
@ -344,7 +324,7 @@
<window_info anchor="bottom" id="Docker" order="7" show_stripe_button="false" />
<window_info anchor="bottom" id="Version Control" order="8" />
<window_info anchor="bottom" id="Inspection Results" order="9" weight="0.32979318" />
<window_info anchor="bottom" id="Terminal" order="10" visible="true" weight="0.32924467" />
<window_info active="true" anchor="bottom" id="Terminal" order="10" visible="true" weight="0.32989025" />
<window_info anchor="bottom" id="Event Log" order="11" side_tool="true" />
<window_info anchor="right" id="Commander" order="0" weight="0.4" />
<window_info anchor="right" id="Ant Build" order="1" weight="0.25" />
@ -589,9 +569,23 @@
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/adder/tests/common/mod.rs">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="48">
<caret line="3" lean-forward="true" selection-start-line="3" selection-end-line="3" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/adder/tests/integration_test.rs">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="144">
<caret line="9" lean-forward="true" selection-start-line="9" selection-end-line="9" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/adder/src/lib.rs">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="1025">
<state relative-caret-position="1056">
<caret line="66" column="5" selection-start-line="66" selection-start-column="5" selection-end-line="66" selection-end-column="5" />
<folding>
<element signature="e#251#252#0" expanded="true" />
@ -612,17 +606,24 @@
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/adder/tests/common/mod.rs">
<entry file="file://$PROJECT_DIR$/minigrep/poem.txt">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="48">
<caret line="3" lean-forward="true" selection-start-line="3" selection-end-line="3" />
<state relative-caret-position="128">
<caret line="8" column="19" selection-start-line="8" selection-start-column="19" selection-end-line="8" selection-end-column="19" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/adder/tests/integration_test.rs">
<entry file="file://$PROJECT_DIR$/minigrep/src/lib.rs">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="96">
<caret line="6" column="20" selection-start-line="6" selection-start-column="20" selection-end-line="6" selection-end-column="20" />
<state relative-caret-position="517">
<caret line="33" column="6" selection-start-line="33" selection-start-column="6" selection-end-line="33" selection-end-column="6" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/minigrep/src/main.rs">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="320">
<caret line="20" lean-forward="true" selection-start-line="20" selection-end-line="20" />
</state>
</provider>
</entry>

7
minigrep/Cargo.toml Normal file
View File

@ -0,0 +1,7 @@
[package]
name = "minigrep"
version = "0.1.0"
authors = ["Timothy Warren <twarren@nabancard.com>"]
edition = "2018"
[dependencies]

9
minigrep/poem.txt Normal file
View 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!
They'd 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!

101
minigrep/src/lib.rs Normal file
View File

@ -0,0 +1,101 @@
use std::env;
use std::error::Error;
use std::fs;
pub struct Config {
pub query: String,
pub filename: String,
pub case_sensitive: bool,
}
impl Config {
pub fn new(args: &[String]) -> Result<Config, &'static str> {
if args.len() < 3 {
return Err("not enough arguments");
}
let query = args[1].clone();
let filename = args[2].clone();
let case_sensitive = env::var("CASE_INSENSITIVE").is_err();
Ok(Config { query, filename, case_sensitive })
}
}
pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
// The ? operator passes the error back to the caller.
let contents = fs::read_to_string(config.filename)?;
let results = if config.case_sensitive {
search(&config.query, &contents)
} else {
search_case_insensitive(&config.query, &contents)
};
for line in results {
println!("{}", line);
}
Ok(())
}
fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
let mut results = Vec::new();
for line in contents.lines() {
if line.contains(query) {
results.push(line)
}
}
results
}
fn search_case_insensitive<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
let query = query.to_lowercase();
let mut results = Vec::new();
for line in contents.lines() {
if line.to_lowercase().contains(&query) {
results.push(line)
}
}
results
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn case_sensitive() {
let query = "duct";
let contents = "\
Rust:
safe, fast, productive.
Pick three.
Duct tape.";
assert_eq!(
vec!["safe, fast, productive."],
search(query, contents)
);
}
#[test]
fn case_insensitive() {
let query = "rUsT";
let contents = "\
Rust:
safe, fast, productive.
Pick three.
Trust me.";
assert_eq!(
vec!["Rust:", "Trust me."],
search_case_insensitive(query, contents)
);
}
}

20
minigrep/src/main.rs Normal file
View File

@ -0,0 +1,20 @@
use std::env;
use std::process;
use minigrep;
use minigrep::Config;
fn main() {
// collect turns an iterator into a collection
let args: Vec<String> = env::args().collect();
let config = Config::new(&args).unwrap_or_else(|err| {
eprintln!("Problem parsing arguments: {}", err);
process::exit(1);
});
if let Err(e) = minigrep::run(config) {
eprintln!("Application error: {}", e);
process::exit(1);
}
}