Add more example code to closure project

This commit is contained in:
Timothy Warren 2019-02-01 16:07:07 -05:00
parent 4015bdac47
commit d4835dc6b8
2 changed files with 144 additions and 38 deletions

107
.idea/workspace.xml generated
View File

@ -2,8 +2,6 @@
<project version="4"> <project version="4">
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="c8f42924-1cd2-4b1c-bcff-602a3368bb16" name="Default Changelist" comment=""> <list default="true" id="c8f42924-1cd2-4b1c-bcff-602a3368bb16" name="Default Changelist" comment="">
<change beforePath="$PROJECT_DIR$/.idea/misc.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/rust.iml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/rust.iml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
</list> </list>
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" /> <option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
@ -15,10 +13,14 @@
<component name="FileEditorManager"> <component name="FileEditorManager">
<leaf> <leaf>
<file pinned="false" current-in-tab="true"> <file pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/closures/src/main.rs"> <entry file="file://$PROJECT_DIR$/closures/src/lib.rs">
<provider selected="true" editor-type-id="text-editor"> <provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="1136"> <state relative-caret-position="1165">
<caret line="71" lean-forward="true" selection-start-line="71" selection-end-line="71" /> <caret line="74" column="1" selection-start-line="74" selection-start-column="1" selection-end-line="74" selection-end-column="1" />
<folding>
<element signature="e#437#438#0" expanded="true" />
<element signature="e#472#473#0" expanded="true" />
</folding>
</state> </state>
</provider> </provider>
</entry> </entry>
@ -82,6 +84,7 @@
<option value="$PROJECT_DIR$/minigrep/src/lib.rs" /> <option value="$PROJECT_DIR$/minigrep/src/lib.rs" />
<option value="$PROJECT_DIR$/minigrep/src/main.rs" /> <option value="$PROJECT_DIR$/minigrep/src/main.rs" />
<option value="$PROJECT_DIR$/closures/src/main.rs" /> <option value="$PROJECT_DIR$/closures/src/main.rs" />
<option value="$PROJECT_DIR$/closures/src/lib.rs" />
</list> </list>
</option> </option>
</component> </component>
@ -112,6 +115,17 @@
<item name="rust" type="3d21c010:ScopeViewTreeModel$ProjectNode" /> <item name="rust" type="3d21c010:ScopeViewTreeModel$ProjectNode" />
<item name="rust" type="2674bda8:ScopeViewTreeModel$GroupNode" /> <item name="rust" type="2674bda8:ScopeViewTreeModel$GroupNode" />
</path> </path>
<path>
<item name="rust" type="3d21c010:ScopeViewTreeModel$ProjectNode" />
<item name="rust" type="2674bda8:ScopeViewTreeModel$GroupNode" />
<item name="closures" type="9f88c78c:ScopeViewTreeModel$FileNode" />
</path>
<path>
<item name="rust" type="3d21c010:ScopeViewTreeModel$ProjectNode" />
<item name="rust" type="2674bda8:ScopeViewTreeModel$GroupNode" />
<item name="closures" type="9f88c78c:ScopeViewTreeModel$FileNode" />
<item name="src" type="9f88c78c:ScopeViewTreeModel$FileNode" />
</path>
</expand> </expand>
<select /> <select />
</subPane> </subPane>
@ -143,7 +157,7 @@
</component> </component>
<component name="PropertiesComponent"> <component name="PropertiesComponent">
<property name="JavaScriptWeakerCompletionTypeGuess" value="true" /> <property name="JavaScriptWeakerCompletionTypeGuess" value="true" />
<property name="com.android.tools.idea.instantapp.provision.ProvisionBeforeRunTaskProvider.myTimeStamp" value="1549047706729" /> <property name="com.android.tools.idea.instantapp.provision.ProvisionBeforeRunTaskProvider.myTimeStamp" value="1549055111185" />
<property name="javascript.nodejs.core.library.configured.version" value="7.1.0" /> <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.eslintPackage" value="$USER_HOME$/.yarn-config/global/node_modules/.bin/eslint" />
<property name="js.eslint.nodeInterpreter" value="project" /> <property name="js.eslint.nodeInterpreter" value="project" />
@ -171,7 +185,7 @@
</list> </list>
</option> </option>
</component> </component>
<component name="RunManager" selected="Cargo Command.Run closures"> <component name="RunManager" selected="Cargo Command.Test using_other_iterator_trait_methods">
<configuration name="&lt;template&gt;" type="TestNG" default="true" selected="false"> <configuration name="&lt;template&gt;" type="TestNG" default="true" selected="false">
<option name="MAIN_CLASS_NAME" /> <option name="MAIN_CLASS_NAME" />
<option name="VM_PARAMETERS" value="-ea" /> <option name="VM_PARAMETERS" value="-ea" />
@ -200,6 +214,26 @@
<envs /> <envs />
<method v="2" /> <method v="2" />
</configuration> </configuration>
<configuration name="Test calling_next_directly" type="CargoCommandRunConfiguration" factoryName="Cargo Command" temporary="true">
<option name="channel" value="DEFAULT" />
<option name="command" value="test --package closures --lib calling_next_directly -- --exact" />
<option name="allFeatures" value="false" />
<option name="nocapture" value="false" />
<option name="backtrace" value="SHORT" />
<option name="workingDirectory" value="file://$PROJECT_DIR$/closures" />
<envs />
<method v="2" />
</configuration>
<configuration name="Test filters_by_size" type="CargoCommandRunConfiguration" factoryName="Cargo Command" temporary="true">
<option name="channel" value="DEFAULT" />
<option name="command" value="test --package closures --lib filters_by_size -- --exact" />
<option name="allFeatures" value="false" />
<option name="nocapture" value="false" />
<option name="backtrace" value="SHORT" />
<option name="workingDirectory" value="file://$PROJECT_DIR$/closures" />
<envs />
<method v="2" />
</configuration>
<configuration name="Test lib::tests" type="CargoCommandRunConfiguration" factoryName="Cargo Command" temporary="true"> <configuration name="Test lib::tests" type="CargoCommandRunConfiguration" factoryName="Cargo Command" temporary="true">
<option name="channel" value="DEFAULT" /> <option name="channel" value="DEFAULT" />
<option name="command" value="test --package adder --lib tests" /> <option name="command" value="test --package adder --lib tests" />
@ -210,33 +244,13 @@
<envs /> <envs />
<method v="2" /> <method v="2" />
</configuration> </configuration>
<configuration name="Test tests::greater_than_100" type="CargoCommandRunConfiguration" factoryName="Cargo Command" temporary="true"> <configuration name="Test using_other_iterator_trait_methods" type="CargoCommandRunConfiguration" factoryName="Cargo Command" temporary="true">
<option name="channel" value="DEFAULT" /> <option name="channel" value="DEFAULT" />
<option name="command" value="test --package adder --lib tests::greater_than_100 -- --exact" /> <option name="command" value="test --package closures --lib using_other_iterator_trait_methods -- --exact" />
<option name="allFeatures" value="false" /> <option name="allFeatures" value="false" />
<option name="nocapture" value="false" /> <option name="nocapture" value="false" />
<option name="backtrace" value="SHORT" /> <option name="backtrace" value="SHORT" />
<option name="workingDirectory" value="file://$PROJECT_DIR$/adder" /> <option name="workingDirectory" value="file://$PROJECT_DIR$/closures" />
<envs />
<method v="2" />
</configuration>
<configuration name="Test tests::greeting_contains_name" type="CargoCommandRunConfiguration" factoryName="Cargo Command" temporary="true">
<option name="channel" value="DEFAULT" />
<option name="command" value="test --package adder --lib tests::greeting_contains_name -- --exact" />
<option name="allFeatures" value="false" />
<option name="nocapture" value="false" />
<option name="backtrace" value="SHORT" />
<option name="workingDirectory" value="file://$PROJECT_DIR$/adder" />
<envs />
<method v="2" />
</configuration>
<configuration name="Test tests::it_works" type="CargoCommandRunConfiguration" factoryName="Cargo Command" temporary="true">
<option name="channel" value="DEFAULT" />
<option name="command" value="test --package adder --lib tests::it_works -- --exact" />
<option name="allFeatures" value="false" />
<option name="nocapture" value="false" />
<option name="backtrace" value="SHORT" />
<option name="workingDirectory" value="file://$PROJECT_DIR$/adder" />
<envs /> <envs />
<method v="2" /> <method v="2" />
</configuration> </configuration>
@ -249,11 +263,11 @@
</configuration> </configuration>
<recent_temporary> <recent_temporary>
<list> <list>
<item itemvalue="Cargo Command.Test using_other_iterator_trait_methods" />
<item itemvalue="Cargo Command.Test calling_next_directly" />
<item itemvalue="Cargo Command.Test filters_by_size" />
<item itemvalue="Cargo Command.Run closures" /> <item itemvalue="Cargo Command.Run closures" />
<item itemvalue="Cargo Command.Test lib::tests" /> <item itemvalue="Cargo Command.Test lib::tests" />
<item itemvalue="Cargo Command.Test tests::greater_than_100" />
<item itemvalue="Cargo Command.Test tests::greeting_contains_name" />
<item itemvalue="Cargo Command.Test tests::it_works" />
</list> </list>
</recent_temporary> </recent_temporary>
</component> </component>
@ -276,13 +290,19 @@
<history-entry file="Test_lib__tests - 2019.01.31 at 14h 59m 57s.xml"> <history-entry file="Test_lib__tests - 2019.01.31 at 14h 59m 57s.xml">
<configuration name="Test lib::tests" configurationId="CargoCommandRunConfiguration" /> <configuration name="Test lib::tests" configurationId="CargoCommandRunConfiguration" />
</history-entry> </history-entry>
<history-entry file="Test_filters_by_size - 2019.02.01 at 15h 58m 53s.xml">
<configuration name="Test filters_by_size" configurationId="CargoCommandRunConfiguration" />
</history-entry>
<history-entry file="Test_calling_next_directly - 2019.02.01 at 16h 04m 05s.xml">
<configuration name="Test calling_next_directly" configurationId="CargoCommandRunConfiguration" />
</history-entry>
</component> </component>
<component name="ToolWindowManager"> <component name="ToolWindowManager">
<frame x="1920" y="-438" width="1080" height="1897" extended-state="6" /> <frame x="1920" y="-438" width="1080" height="1897" extended-state="6" />
<editor active="true" /> <editor active="true" />
<layout> <layout>
<window_info active="true" content_ui="combo" id="Project" order="0" sideWeight="0.49692565" visible="true" weight="0.2540132" /> <window_info content_ui="combo" id="Project" order="0" sideWeight="0.49642006" visible="true" weight="0.2540132" />
<window_info id="Structure" order="1" sideWeight="0.50307435" side_tool="true" visible="true" weight="0.2540132" /> <window_info id="Structure" order="1" sideWeight="0.50358" side_tool="true" visible="true" weight="0.2540132" />
<window_info id="Image Layers" order="2" /> <window_info id="Image Layers" order="2" />
<window_info id="Designer" order="3" /> <window_info id="Designer" order="3" />
<window_info id="Capture Tool" order="4" /> <window_info id="Capture Tool" order="4" />
@ -290,7 +310,7 @@
<window_info id="Cargo" order="6" sideWeight="0.49791494" weight="0.3286119" /> <window_info id="Cargo" order="6" sideWeight="0.49791494" weight="0.3286119" />
<window_info anchor="bottom" id="Message" order="0" /> <window_info anchor="bottom" id="Message" order="0" />
<window_info anchor="bottom" id="Find" order="1" weight="0.32979318" /> <window_info anchor="bottom" id="Find" order="1" weight="0.32979318" />
<window_info anchor="bottom" id="Run" order="2" weight="0.29737285" /> <window_info active="true" anchor="bottom" id="Run" order="2" visible="true" weight="0.29737285" />
<window_info anchor="bottom" id="Debug" order="3" weight="0.4" /> <window_info anchor="bottom" id="Debug" order="3" weight="0.4" />
<window_info anchor="bottom" id="Cvs" order="4" weight="0.25" /> <window_info anchor="bottom" id="Cvs" order="4" weight="0.25" />
<window_info anchor="bottom" id="Inspection" order="5" weight="0.4" /> <window_info anchor="bottom" id="Inspection" order="5" weight="0.4" />
@ -603,8 +623,19 @@
</entry> </entry>
<entry file="file://$PROJECT_DIR$/closures/src/main.rs"> <entry file="file://$PROJECT_DIR$/closures/src/main.rs">
<provider selected="true" editor-type-id="text-editor"> <provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="1136"> <state relative-caret-position="1120">
<caret line="71" lean-forward="true" selection-start-line="71" selection-end-line="71" /> <caret line="70" column="1" selection-end-line="71" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/closures/src/lib.rs">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="1165">
<caret line="74" column="1" selection-start-line="74" selection-start-column="1" selection-end-line="74" selection-end-column="1" />
<folding>
<element signature="e#437#438#0" expanded="true" />
<element signature="e#472#473#0" expanded="true" />
</folding>
</state> </state>
</provider> </provider>
</entry> </entry>

75
closures/src/lib.rs Normal file
View File

@ -0,0 +1,75 @@
#[derive(PartialEq, Debug)]
struct Shoe {
size: u32,
style: String,
}
fn shoes_in_my_size(shoes: Vec<Shoe>, shoe_size: u32) -> Vec<Shoe> {
shoes.into_iter() // into_iter takes ownership of the original vector
.filter(|s| s.size == shoe_size) // Filter is lazy!
.collect() // Tell the iterator to run, and give me a collection
}
struct Counter {
count: u32,
}
impl Counter {
fn new() -> Counter {
Counter { count: 0 }
}
}
impl Iterator for Counter {
type Item = u32;
fn next(&mut self) -> Option<Self::Item> {
self.count += 1;
if self.count < 6 {
Some(self.count)
} else {
None
}
}
}
#[test]
fn filters_by_size() {
let shoes = vec![
Shoe { size: 10, style: String::from("sneaker") },
Shoe { size: 13, style: String::from("sandal") },
Shoe { size: 10, style: String::from("boot") },
];
let in_my_size = shoes_in_my_size(shoes, 10);
assert_eq!(
in_my_size,
vec![
Shoe { size: 10, style: String::from("sneaker") },
Shoe { size: 10, style: String::from("boot") },
]
);
}
#[test]
fn calling_next_directly() {
let mut counter = Counter::new();
assert_eq!(counter.next(), Some(1));
assert_eq!(counter.next(), Some(2));
assert_eq!(counter.next(), Some(3));
assert_eq!(counter.next(), Some(4));
assert_eq!(counter.next(), Some(5));
assert_eq!(counter.next(), None);
}
#[test]
fn using_other_iterator_trait_methods() {
let sum: u32 = Counter::new().zip(Counter::new().skip(1))
.map(|(a, b)| a * b)
.filter(|x| x % 3 == 0)
.sum();
assert_eq!(18, sum);
}