Add lifetime generic example
This commit is contained in:
parent
abdd1da877
commit
65f88596cb
1
.idea/misc.xml
generated
1
.idea/misc.xml
generated
@ -18,6 +18,7 @@
|
||||
<cargoProject FILE="$PROJECT_DIR$/errors/Cargo.toml" />
|
||||
<cargoProject FILE="$PROJECT_DIR$/largest_number/src/main.rs" />
|
||||
<cargoProject FILE="$PROJECT_DIR$/generics/Cargo.toml" />
|
||||
<cargoProject FILE="$PROJECT_DIR$/lifetimes/Cargo.toml" />
|
||||
</component>
|
||||
<component name="ComposerJsonPluginSettings">
|
||||
<unboundedVersionInspectionSettings>
|
||||
|
5
.idea/rust.iml
generated
5
.idea/rust.iml
generated
@ -70,6 +70,10 @@
|
||||
<sourceFolder url="file://$MODULE_DIR$/generics/examples" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/generics/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/generics/benches" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/lifetimes/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/lifetimes/examples" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/lifetimes/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/lifetimes/benches" isTestSource="true" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/branches/target" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/ctof/target" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/enums/target" />
|
||||
@ -78,6 +82,7 @@
|
||||
<excludeFolder url="file://$MODULE_DIR$/functions/target" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/generics/target" />
|
||||
<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$/modules/target" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/rectangles/target" />
|
||||
|
94
.idea/workspace.xml
generated
94
.idea/workspace.xml
generated
@ -14,42 +14,14 @@
|
||||
</component>
|
||||
<component name="FileEditorManager">
|
||||
<leaf SIDE_TABS_SIZE_LIMIT_KEY="300">
|
||||
<file pinned="false" current-in-tab="false">
|
||||
<entry file="file://$PROJECT_DIR$/generics/src/main.rs">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="192">
|
||||
<caret line="12" column="9" lean-forward="true" selection-start-line="12" selection-start-column="9" selection-end-line="12" selection-end-column="9" />
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
</file>
|
||||
<file pinned="false" current-in-tab="false">
|
||||
<entry file="file://$PROJECT_DIR$/generics/src/point.rs">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="544">
|
||||
<caret line="34" lean-forward="true" selection-start-line="34" selection-end-line="34" />
|
||||
<folding>
|
||||
<element signature="e#548#549#0" expanded="true" />
|
||||
<element signature="e#570#571#0" expanded="true" />
|
||||
<element signature="e#665#666#0" expanded="true" />
|
||||
<element signature="e#720#721#0" expanded="true" />
|
||||
</folding>
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
</file>
|
||||
<file pinned="false" current-in-tab="true">
|
||||
<entry file="file://$PROJECT_DIR$/generics/src/lib.rs">
|
||||
<entry file="file://$PROJECT_DIR$/lifetimes/src/main.rs">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="256">
|
||||
<caret line="16" column="54" selection-start-line="16" selection-start-column="54" selection-end-line="16" selection-end-column="54" />
|
||||
<state relative-caret-position="912">
|
||||
<caret line="57" lean-forward="true" selection-start-line="57" selection-end-line="57" />
|
||||
<folding>
|
||||
<element signature="e#111#112#0" expanded="true" />
|
||||
<element signature="e#127#128#0" expanded="true" />
|
||||
<element signature="e#171#172#0" expanded="true" />
|
||||
<element signature="e#382#383#0" expanded="true" />
|
||||
<element signature="e#420#421#0" expanded="true" />
|
||||
<element signature="e#782#783#0" expanded="true" />
|
||||
<element signature="e#826#827#0" expanded="true" />
|
||||
</folding>
|
||||
</state>
|
||||
</provider>
|
||||
@ -104,6 +76,7 @@
|
||||
<option value="$PROJECT_DIR$/generics/src/main.rs" />
|
||||
<option value="$PROJECT_DIR$/generics/src/point.rs" />
|
||||
<option value="$PROJECT_DIR$/generics/src/lib.rs" />
|
||||
<option value="$PROJECT_DIR$/lifetimes/src/main.rs" />
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
@ -137,12 +110,12 @@
|
||||
<path>
|
||||
<item name="rust" type="3d21c010:ScopeViewTreeModel$ProjectNode" />
|
||||
<item name="rust" type="2674bda8:ScopeViewTreeModel$GroupNode" />
|
||||
<item name="generics" type="9f88c78c:ScopeViewTreeModel$FileNode" />
|
||||
<item name="lifetimes" type="9f88c78c:ScopeViewTreeModel$FileNode" />
|
||||
</path>
|
||||
<path>
|
||||
<item name="rust" type="3d21c010:ScopeViewTreeModel$ProjectNode" />
|
||||
<item name="rust" type="2674bda8:ScopeViewTreeModel$GroupNode" />
|
||||
<item name="generics" type="9f88c78c:ScopeViewTreeModel$FileNode" />
|
||||
<item name="lifetimes" type="9f88c78c:ScopeViewTreeModel$FileNode" />
|
||||
<item name="src" type="9f88c78c:ScopeViewTreeModel$FileNode" />
|
||||
</path>
|
||||
</expand>
|
||||
@ -176,7 +149,7 @@
|
||||
</component>
|
||||
<component name="PropertiesComponent">
|
||||
<property name="JavaScriptWeakerCompletionTypeGuess" value="true" />
|
||||
<property name="com.android.tools.idea.instantapp.provision.ProvisionBeforeRunTaskProvider.myTimeStamp" value="1548952830934" />
|
||||
<property name="com.android.tools.idea.instantapp.provision.ProvisionBeforeRunTaskProvider.myTimeStamp" value="1548960808738" />
|
||||
<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" />
|
||||
@ -204,7 +177,7 @@
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
<component name="RunManager" selected="Cargo Command.Run generics">
|
||||
<component name="RunManager" selected="Cargo Command.Run lifetimes">
|
||||
<configuration name="<template>" type="TestNG" default="true" selected="false">
|
||||
<option name="MAIN_CLASS_NAME" />
|
||||
<option name="VM_PARAMETERS" value="-ea" />
|
||||
@ -223,16 +196,6 @@
|
||||
<option name="Make" enabled="true" />
|
||||
</method>
|
||||
</configuration>
|
||||
<configuration name="Run enums" type="CargoCommandRunConfiguration" factoryName="Cargo Command" temporary="true">
|
||||
<option name="channel" value="DEFAULT" />
|
||||
<option name="command" value="run --package enums --bin enums" />
|
||||
<option name="allFeatures" value="false" />
|
||||
<option name="nocapture" value="false" />
|
||||
<option name="backtrace" value="SHORT" />
|
||||
<option name="workingDirectory" value="file://$PROJECT_DIR$/enums" />
|
||||
<envs />
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
<configuration name="Run errors" type="CargoCommandRunConfiguration" factoryName="Cargo Command" temporary="true">
|
||||
<option name="channel" value="DEFAULT" />
|
||||
<option name="command" value="run --package errors --bin errors" />
|
||||
@ -263,6 +226,16 @@
|
||||
<envs />
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
<configuration name="Run lifetimes" type="CargoCommandRunConfiguration" factoryName="Cargo Command" temporary="true">
|
||||
<option name="channel" value="DEFAULT" />
|
||||
<option name="command" value="run --package lifetimes --bin lifetimes" />
|
||||
<option name="allFeatures" value="false" />
|
||||
<option name="nocapture" value="false" />
|
||||
<option name="backtrace" value="SHORT" />
|
||||
<option name="workingDirectory" value="file://$PROJECT_DIR$/lifetimes" />
|
||||
<envs />
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
<configuration name="Run modules" type="CargoCommandRunConfiguration" factoryName="Cargo Command" temporary="true">
|
||||
<option name="channel" value="DEFAULT" />
|
||||
<option name="command" value="run --package modules --bin modules" />
|
||||
@ -282,11 +255,11 @@
|
||||
</configuration>
|
||||
<recent_temporary>
|
||||
<list>
|
||||
<item itemvalue="Cargo Command.Run lifetimes" />
|
||||
<item itemvalue="Cargo Command.Run generics" />
|
||||
<item itemvalue="Cargo Command.Run largest_number" />
|
||||
<item itemvalue="Cargo Command.Run errors" />
|
||||
<item itemvalue="Cargo Command.Run modules" />
|
||||
<item itemvalue="Cargo Command.Run enums" />
|
||||
</list>
|
||||
</recent_temporary>
|
||||
</component>
|
||||
@ -510,6 +483,21 @@
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/generics/src/lib.rs">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="128">
|
||||
<caret line="8" column="32" lean-forward="true" selection-start-line="8" selection-start-column="32" selection-end-line="8" selection-end-column="32" />
|
||||
<folding>
|
||||
<element signature="e#127#128#0" expanded="true" />
|
||||
<element signature="e#171#172#0" expanded="true" />
|
||||
<element signature="e#382#383#0" expanded="true" />
|
||||
<element signature="e#420#421#0" expanded="true" />
|
||||
<element signature="e#782#783#0" expanded="true" />
|
||||
<element signature="e#826#827#0" expanded="true" />
|
||||
</folding>
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/generics/src/point.rs">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="544">
|
||||
@ -530,17 +518,13 @@
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/generics/src/lib.rs">
|
||||
<entry file="file://$PROJECT_DIR$/lifetimes/src/main.rs">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="256">
|
||||
<caret line="16" column="54" selection-start-line="16" selection-start-column="54" selection-end-line="16" selection-end-column="54" />
|
||||
<state relative-caret-position="912">
|
||||
<caret line="57" lean-forward="true" selection-start-line="57" selection-end-line="57" />
|
||||
<folding>
|
||||
<element signature="e#111#112#0" expanded="true" />
|
||||
<element signature="e#127#128#0" expanded="true" />
|
||||
<element signature="e#171#172#0" expanded="true" />
|
||||
<element signature="e#382#383#0" expanded="true" />
|
||||
<element signature="e#420#421#0" expanded="true" />
|
||||
<element signature="e#782#783#0" expanded="true" />
|
||||
<element signature="e#826#827#0" expanded="true" />
|
||||
</folding>
|
||||
</state>
|
||||
</provider>
|
||||
|
7
lifetimes/Cargo.toml
Normal file
7
lifetimes/Cargo.toml
Normal file
@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "lifetimes"
|
||||
version = "0.1.0"
|
||||
authors = ["Timothy Warren <twarren@nabancard.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
57
lifetimes/src/main.rs
Normal file
57
lifetimes/src/main.rs
Normal file
@ -0,0 +1,57 @@
|
||||
use std::fmt::Display;
|
||||
|
||||
struct ImportantExcerpt<'a> {
|
||||
part: &'a str,
|
||||
}
|
||||
|
||||
impl<'a> ImportantExcerpt<'a> {
|
||||
fn level(&self) -> i32 {
|
||||
3
|
||||
}
|
||||
|
||||
fn announce_and_return_part(&self, announcement: &str) -> &str {
|
||||
println!("Attention please: {}", announcement);
|
||||
self.part
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let string1 = String::from("abcd");
|
||||
let string2 = "xyz";
|
||||
|
||||
let result = longest(string1.as_str(), string2);
|
||||
println!("The longest string is {}", result);
|
||||
|
||||
let string1 = String::from("long string is long");
|
||||
|
||||
{
|
||||
let string2 = String::from("xyz");
|
||||
let result = longest(string1.as_str(), string2.as_str());
|
||||
println!("The longest string is {}", result);
|
||||
}
|
||||
|
||||
let novel = String::from("Call me Ishmael. Some years ago...");
|
||||
let first_sentence = novel.split('.')
|
||||
.next()
|
||||
.expect("Could not find a '.'");
|
||||
let _i = ImportantExcerpt { part: first_sentence };
|
||||
}
|
||||
|
||||
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
|
||||
if x.len() > y.len() {
|
||||
x
|
||||
} else {
|
||||
y
|
||||
}
|
||||
}
|
||||
|
||||
fn longest_with_an_announcement<'a, T>(x: &'a str, y: &'a str, ann: T) -> &'a str
|
||||
where T: Display
|
||||
{
|
||||
println!("Announcement! {}", ann);
|
||||
if x.len() > y.len() {
|
||||
x
|
||||
} else {
|
||||
y
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user