Add pattern_matching examples
This commit is contained in:
parent
36ed658b19
commit
6d65d88b76
@ -35,6 +35,7 @@
|
||||
<cargoProject FILE="$PROJECT_DIR$/trait_objects/Cargo.toml" />
|
||||
<cargoProject FILE="$PROJECT_DIR$/gui/Cargo.toml" />
|
||||
<cargoProject FILE="$PROJECT_DIR$/blog/Cargo.toml" />
|
||||
<cargoProject FILE="$PROJECT_DIR$/pattern_matching/Cargo.toml" />
|
||||
</component>
|
||||
<component name="ComposerJsonPluginSettings">
|
||||
<unboundedVersionInspectionSettings>
|
||||
|
@ -141,6 +141,10 @@
|
||||
<sourceFolder url="file://$MODULE_DIR$/blog/examples" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/blog/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/blog/benches" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/pattern_matching/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/pattern_matching/examples" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/pattern_matching/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/pattern_matching/benches" isTestSource="true" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/add/add-one/target" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/add/adder/target" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/adder/target" />
|
||||
@ -163,6 +167,7 @@
|
||||
<excludeFolder url="file://$MODULE_DIR$/modules/target" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/mutex/target" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/oop/target" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/pattern_matching/target" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/rectangles/target" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/reference_counting/target" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/reference_cycles/target" />
|
||||
|
@ -2,8 +2,9 @@
|
||||
<project version="4">
|
||||
<component name="ChangeListManager">
|
||||
<list default="true" id="c8f42924-1cd2-4b1c-bcff-602a3368bb16" name="Default Changelist" comment="">
|
||||
<change beforePath="$PROJECT_DIR$/blog/src/lib.rs" beforeDir="false" afterPath="$PROJECT_DIR$/blog/src/lib.rs" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/blog/src/main.rs" beforeDir="false" afterPath="$PROJECT_DIR$/blog/src/main.rs" afterDir="false" />
|
||||
<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" />
|
||||
</list>
|
||||
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
|
||||
<option name="SHOW_DIALOG" value="false" />
|
||||
@ -14,25 +15,10 @@
|
||||
<component name="FileEditorManager">
|
||||
<leaf>
|
||||
<file pinned="false" current-in-tab="true">
|
||||
<entry file="file://$PROJECT_DIR$/blog/src/main.rs">
|
||||
<entry file="file://$PROJECT_DIR$/pattern_matching/src/main.rs">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="176">
|
||||
<caret line="11" column="64" selection-start-line="11" selection-start-column="64" selection-end-line="11" selection-end-column="64" />
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
</file>
|
||||
<file pinned="false" current-in-tab="false">
|
||||
<entry file="file://$PROJECT_DIR$/blog/src/lib.rs">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="672">
|
||||
<caret line="42" column="1" lean-forward="true" selection-start-line="42" selection-start-column="1" selection-end-line="42" selection-end-column="1" />
|
||||
<folding>
|
||||
<element signature="e#240#241#0" expanded="true" />
|
||||
<element signature="e#268#269#0" expanded="true" />
|
||||
<element signature="e#656#657#0" expanded="true" />
|
||||
<element signature="e#699#700#0" expanded="true" />
|
||||
</folding>
|
||||
<state relative-caret-position="903">
|
||||
<caret line="194" column="24" selection-start-line="194" selection-start-column="24" selection-end-line="194" selection-end-column="24" />
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
@ -66,8 +52,6 @@
|
||||
<component name="IdeDocumentHistory">
|
||||
<option name="CHANGED_PATHS">
|
||||
<list>
|
||||
<option value="$PROJECT_DIR$/slices/src/main.rs" />
|
||||
<option value="$PROJECT_DIR$/structs/src/main.rs" />
|
||||
<option value="$PROJECT_DIR$/rectangles/src/main.rs" />
|
||||
<option value="$PROJECT_DIR$/enums/src/main.rs" />
|
||||
<option value="$PROJECT_DIR$/modules/src/sound.rs" />
|
||||
@ -117,6 +101,8 @@
|
||||
<option value="$PROJECT_DIR$/gui/src/lib.rs" />
|
||||
<option value="$PROJECT_DIR$/blog/src/lib.rs" />
|
||||
<option value="$PROJECT_DIR$/blog/src/main.rs" />
|
||||
<option value="$PROJECT_DIR$/blog/Cargo.toml" />
|
||||
<option value="$PROJECT_DIR$/pattern_matching/src/main.rs" />
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
@ -150,23 +136,12 @@
|
||||
<path>
|
||||
<item name="rust" type="3d21c010:ScopeViewTreeModel$ProjectNode" />
|
||||
<item name="rust" type="2674bda8:ScopeViewTreeModel$GroupNode" />
|
||||
<item name="blog" type="9f88c78c:ScopeViewTreeModel$FileNode" />
|
||||
<item name="pattern_matching" type="9f88c78c:ScopeViewTreeModel$FileNode" />
|
||||
</path>
|
||||
<path>
|
||||
<item name="rust" type="3d21c010:ScopeViewTreeModel$ProjectNode" />
|
||||
<item name="rust" type="2674bda8:ScopeViewTreeModel$GroupNode" />
|
||||
<item name="blog" type="9f88c78c:ScopeViewTreeModel$FileNode" />
|
||||
<item name="src" type="9f88c78c:ScopeViewTreeModel$FileNode" />
|
||||
</path>
|
||||
<path>
|
||||
<item name="rust" type="3d21c010:ScopeViewTreeModel$ProjectNode" />
|
||||
<item name="rust" type="2674bda8:ScopeViewTreeModel$GroupNode" />
|
||||
<item name="twelve_days_of_christmas" type="9f88c78c:ScopeViewTreeModel$FileNode" />
|
||||
</path>
|
||||
<path>
|
||||
<item name="rust" type="3d21c010:ScopeViewTreeModel$ProjectNode" />
|
||||
<item name="rust" type="2674bda8:ScopeViewTreeModel$GroupNode" />
|
||||
<item name="twelve_days_of_christmas" type="9f88c78c:ScopeViewTreeModel$FileNode" />
|
||||
<item name="pattern_matching" type="9f88c78c:ScopeViewTreeModel$FileNode" />
|
||||
<item name="src" type="9f88c78c:ScopeViewTreeModel$FileNode" />
|
||||
</path>
|
||||
</expand>
|
||||
@ -200,7 +175,7 @@
|
||||
</component>
|
||||
<component name="PropertiesComponent">
|
||||
<property name="JavaScriptWeakerCompletionTypeGuess" value="true" />
|
||||
<property name="com.android.tools.idea.instantapp.provision.ProvisionBeforeRunTaskProvider.myTimeStamp" value="1549483257467" />
|
||||
<property name="com.android.tools.idea.instantapp.provision.ProvisionBeforeRunTaskProvider.myTimeStamp" value="1549488475930" />
|
||||
<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" />
|
||||
@ -228,7 +203,7 @@
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
<component name="RunManager" selected="Cargo Command.Run blog">
|
||||
<component name="RunManager" selected="Cargo Command.Run pattern_matching">
|
||||
<configuration name="<template>" type="TestNG" default="true" selected="false">
|
||||
<option name="MAIN_CLASS_NAME" />
|
||||
<option name="VM_PARAMETERS" value="-ea" />
|
||||
@ -267,13 +242,13 @@
|
||||
<envs />
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
<configuration name="Run mutex" type="CargoCommandRunConfiguration" factoryName="Cargo Command" temporary="true">
|
||||
<configuration name="Run pattern_matching" type="CargoCommandRunConfiguration" factoryName="Cargo Command" temporary="true">
|
||||
<option name="channel" value="DEFAULT" />
|
||||
<option name="command" value="run --package mutex --bin mutex" />
|
||||
<option name="command" value="run --package pattern_matching --bin pattern_matching" />
|
||||
<option name="allFeatures" value="false" />
|
||||
<option name="nocapture" value="false" />
|
||||
<option name="backtrace" value="SHORT" />
|
||||
<option name="workingDirectory" value="file://$PROJECT_DIR$/mutex" />
|
||||
<option name="workingDirectory" value="file://$PROJECT_DIR$/pattern_matching" />
|
||||
<envs />
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
@ -306,11 +281,11 @@
|
||||
</configuration>
|
||||
<recent_temporary>
|
||||
<list>
|
||||
<item itemvalue="Cargo Command.Run pattern_matching" />
|
||||
<item itemvalue="Cargo Command.Run blog" />
|
||||
<item itemvalue="Cargo Command.Run gui" />
|
||||
<item itemvalue="Cargo Command.Test lib::tests" />
|
||||
<item itemvalue="Cargo Command.Run twelve_days_of_christmas" />
|
||||
<item itemvalue="Cargo Command.Run mutex" />
|
||||
</list>
|
||||
</recent_temporary>
|
||||
</component>
|
||||
@ -381,24 +356,6 @@
|
||||
<option name="version" value="1" />
|
||||
</component>
|
||||
<component name="editorHistoryManager">
|
||||
<entry file="file://$PROJECT_DIR$/references/src/main.rs">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="368">
|
||||
<caret line="23" lean-forward="true" selection-start-line="23" selection-end-line="23" />
|
||||
<folding>
|
||||
<element signature="e#381#382#0" expanded="true" />
|
||||
<element signature="e#399#400#0" expanded="true" />
|
||||
</folding>
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/scope/src/main.rs">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="352">
|
||||
<caret line="22" column="66" selection-start-line="22" selection-start-column="66" selection-end-line="22" selection-end-column="66" />
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/structs/src/main.rs">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="624">
|
||||
@ -806,7 +763,7 @@
|
||||
<entry file="file://$PROJECT_DIR$/blog/src/lib.rs">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="672">
|
||||
<caret line="42" column="1" lean-forward="true" selection-start-line="42" selection-start-column="1" selection-end-line="42" selection-end-column="1" />
|
||||
<caret line="42" column="1" selection-start-line="42" selection-start-column="1" selection-end-line="42" selection-end-column="1" />
|
||||
<folding>
|
||||
<element signature="e#240#241#0" expanded="true" />
|
||||
<element signature="e#268#269#0" expanded="true" />
|
||||
@ -816,10 +773,24 @@
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/blog/Cargo.toml">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="112">
|
||||
<caret line="7" lean-forward="true" selection-start-line="7" selection-end-line="7" />
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/blog/src/main.rs">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="176">
|
||||
<caret line="11" column="64" selection-start-line="11" selection-start-column="64" selection-end-line="11" selection-end-column="64" />
|
||||
<state relative-caret-position="208">
|
||||
<caret line="13" selection-start-line="13" selection-end-line="13" />
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/pattern_matching/src/main.rs">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="903">
|
||||
<caret line="194" column="24" selection-start-line="194" selection-start-column="24" selection-end-line="194" selection-end-column="24" />
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
|
7
pattern_matching/Cargo.toml
Normal file
7
pattern_matching/Cargo.toml
Normal file
@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "pattern_matching"
|
||||
version = "0.1.0"
|
||||
authors = ["Timothy Warren <twarren@nabancard.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
196
pattern_matching/src/main.rs
Normal file
196
pattern_matching/src/main.rs
Normal file
@ -0,0 +1,196 @@
|
||||
struct Point {
|
||||
x: i32,
|
||||
y: i32,
|
||||
}
|
||||
|
||||
struct XYZPoint {
|
||||
x: i32,
|
||||
y: i32,
|
||||
z: i32,
|
||||
}
|
||||
|
||||
enum Color {
|
||||
Rgb(i32, i32, i32),
|
||||
Hsv(i32, i32, i32)
|
||||
}
|
||||
|
||||
enum Message {
|
||||
Quit,
|
||||
Move { x: i32, y: i32 },
|
||||
Write(String),
|
||||
ChangeColor(Color),
|
||||
}
|
||||
|
||||
fn match_variable_shadowing () {
|
||||
let x = Some(5);
|
||||
let y = 10;
|
||||
|
||||
match x {
|
||||
Some(50) => println!("Got 50"),
|
||||
|
||||
// shadows the outer y, gets the value of x
|
||||
Some(y) => println!("Matched, y = {:?}", y),
|
||||
|
||||
_ => println!("Default case, x = {:?}", x),
|
||||
}
|
||||
|
||||
println!("at the end: x = {:?}, y = {:?}", x, y);
|
||||
}
|
||||
|
||||
fn match_multiple () {
|
||||
let x = 1;
|
||||
|
||||
match x {
|
||||
1 | 2 => println!("one or two"),
|
||||
3 => println!("three"),
|
||||
_ => println!("anything"),
|
||||
}
|
||||
}
|
||||
|
||||
fn match_range () {
|
||||
let x = 5;
|
||||
|
||||
match x {
|
||||
1 ... 5 => println!("one through five"),
|
||||
_ => println!("something else"),
|
||||
}
|
||||
}
|
||||
|
||||
fn match_destructure_with_literals () {
|
||||
let p = Point { x: 0, y: 7 };
|
||||
|
||||
match p {
|
||||
Point { x, y: 0 } => println!("On the x axis at {}", x),
|
||||
Point { x: 0, y } => println!("On the y axis at {}", y),
|
||||
Point { x, y } => println!("On neither axis: ({}, {})", x, y),
|
||||
}
|
||||
}
|
||||
|
||||
fn match_destructure_enum () {
|
||||
let msg = Message::ChangeColor(Color::Hsv(0, 160, 255));
|
||||
|
||||
match msg {
|
||||
Message::ChangeColor(Color::Rgb(r, g, b)) => {
|
||||
println!(
|
||||
"Change the color to red {}, green {}, and blue {}",
|
||||
r,
|
||||
g,
|
||||
b
|
||||
)
|
||||
},
|
||||
Message::ChangeColor(Color::Hsv(h, s, v)) => {
|
||||
println!(
|
||||
"Change the color to hue {}, saturation {}, and value {}",
|
||||
h,
|
||||
s,
|
||||
v
|
||||
)
|
||||
}
|
||||
_ => ()
|
||||
}
|
||||
}
|
||||
|
||||
fn destructure_references() {
|
||||
let points = vec![
|
||||
Point { x: 0, y: 0 },
|
||||
Point { x: 1, y: 5 },
|
||||
Point { x: 10, y: -3 },
|
||||
];
|
||||
|
||||
let sum_of_squares: i32 = points
|
||||
.iter()
|
||||
.map(|&Point { x, y }| x * x + y * y)
|
||||
.sum();
|
||||
}
|
||||
|
||||
fn match_ignore_value () {
|
||||
let mut setting_value = Some(5);
|
||||
let new_setting_value = Some(10);
|
||||
|
||||
match (setting_value, new_setting_value) {
|
||||
(Some(_), Some(_)) => {
|
||||
println!("Can't overwrite an existing customized value");
|
||||
}
|
||||
_ => {
|
||||
setting_value = new_setting_value;
|
||||
}
|
||||
}
|
||||
|
||||
println!("setting is {:?}", setting_value);
|
||||
}
|
||||
|
||||
fn match_ignore_value_parts () {
|
||||
let origin = XYZPoint { x: 0, y: 0, z: 0 };
|
||||
|
||||
match origin {
|
||||
XYZPoint { x, .. } => println!("x is {}", x),
|
||||
}
|
||||
}
|
||||
|
||||
fn match_ignore_value_parts_tuple () {
|
||||
let numbers = (2, 4, 8, 16, 32);
|
||||
|
||||
match numbers {
|
||||
(first, .., last) => {
|
||||
println!("Some numbers: {}, {}", first, last);
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn match_guard() {
|
||||
let num = Some(4);
|
||||
|
||||
match num {
|
||||
Some(x) if x < 5 => println!("less than five: {}", x), // Has a match guard
|
||||
Some(x) => println!("{}", x),
|
||||
None => (),
|
||||
}
|
||||
}
|
||||
|
||||
fn match_variable_shadowing_fixed_with_match_guard() {
|
||||
let x = Some(5);
|
||||
let y = 10;
|
||||
|
||||
match x {
|
||||
Some(50) => println!("Got 50"),
|
||||
Some(n) if n == y => println!("Matched, n = {:?}", n),
|
||||
_ => println!("Default case, x = {:?}", x),
|
||||
}
|
||||
|
||||
println!("at the end: x = {:?}, y = {:?}", x, y);
|
||||
}
|
||||
|
||||
fn at_match_var_bind() {
|
||||
enum Message {
|
||||
Hello { id: i32 },
|
||||
}
|
||||
|
||||
let msg = Message::Hello { id: 5 };
|
||||
|
||||
match msg {
|
||||
Message::Hello { id: id_variable @ 3...7 } => { // The @ binds the value and matches
|
||||
println!("Found an id in range: {}", id_variable)
|
||||
},
|
||||
Message::Hello { id: 10...12 } => {
|
||||
println!("Found an id in another range")
|
||||
},
|
||||
Message::Hello { id } => {
|
||||
println!("Found some other id: {}", id)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
match_variable_shadowing();
|
||||
match_multiple();
|
||||
match_range();
|
||||
match_destructure_with_literals();
|
||||
match_destructure_enum();
|
||||
destructure_references();
|
||||
match_ignore_value();
|
||||
match_ignore_value_parts();
|
||||
match_ignore_value_parts_tuple();
|
||||
match_guard();
|
||||
match_variable_shadowing_fixed_with_match_guard();
|
||||
at_match_var_bind();
|
||||
}
|
Loading…
Reference in New Issue
Block a user