Add generic type examples
This commit is contained in:
parent
17f7e4dfcf
commit
abdd1da877
2
.idea/misc.xml
generated
2
.idea/misc.xml
generated
@ -16,6 +16,8 @@
|
||||
<cargoProject FILE="$PROJECT_DIR$/enums/Cargo.toml" />
|
||||
<cargoProject FILE="$PROJECT_DIR$/modules/Cargo.toml" />
|
||||
<cargoProject FILE="$PROJECT_DIR$/errors/Cargo.toml" />
|
||||
<cargoProject FILE="$PROJECT_DIR$/largest_number/src/main.rs" />
|
||||
<cargoProject FILE="$PROJECT_DIR$/generics/Cargo.toml" />
|
||||
</component>
|
||||
<component name="ComposerJsonPluginSettings">
|
||||
<unboundedVersionInspectionSettings>
|
||||
|
8
.idea/rust.iml
generated
8
.idea/rust.iml
generated
@ -63,12 +63,20 @@
|
||||
<sourceFolder url="file://$MODULE_DIR$/errors/examples" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/errors/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/errors/benches" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/generics/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/largest_number/examples" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/largest_number/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/largest_number/benches" isTestSource="true" />
|
||||
<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" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/branches/target" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/ctof/target" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/enums/target" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/errors/target" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/ftoc/target" />
|
||||
<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$/loops/target" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/modules/target" />
|
||||
|
167
.idea/workspace.xml
generated
167
.idea/workspace.xml
generated
@ -2,7 +2,6 @@
|
||||
<project version="4">
|
||||
<component name="ChangeListManager">
|
||||
<list default="true" id="c8f42924-1cd2-4b1c-bcff-602a3368bb16" name="Default Changelist" comment="">
|
||||
<change beforePath="$PROJECT_DIR$/.gitignore" beforeDir="false" afterPath="$PROJECT_DIR$/.gitignore" 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" />
|
||||
@ -16,23 +15,42 @@
|
||||
<component name="FileEditorManager">
|
||||
<leaf SIDE_TABS_SIZE_LIMIT_KEY="300">
|
||||
<file pinned="false" current-in-tab="false">
|
||||
<entry file="file://$PROJECT_DIR$/errors/src/main.rs">
|
||||
<entry file="file://$PROJECT_DIR$/generics/src/main.rs">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="1024">
|
||||
<caret line="64" column="1" lean-forward="true" selection-start-line="64" selection-start-column="1" selection-end-line="64" selection-end-column="1" />
|
||||
<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#285#286#0" expanded="true" />
|
||||
<element signature="e#323#324#0" expanded="true" />
|
||||
<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$/.gitignore">
|
||||
<entry file="file://$PROJECT_DIR$/generics/src/lib.rs">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="720">
|
||||
<caret line="45" column="16" selection-start-line="45" selection-start-column="16" selection-end-line="45" selection-end-column="16" />
|
||||
<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" />
|
||||
<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>
|
||||
@ -79,6 +97,13 @@
|
||||
<option value="$PROJECT_DIR$/guessing_game/.gitignore" />
|
||||
<option value="$PROJECT_DIR$/errors/src/main.rs" />
|
||||
<option value="$PROJECT_DIR$/.gitignore" />
|
||||
<option value="$PROJECT_DIR$/largest_number/src/main.rs" />
|
||||
<option value="$PROJECT_DIR$/generics/Cargo.toml" />
|
||||
<option value="$PROJECT_DIR$/generics/src/trait.rs" />
|
||||
<option value="$PROJECT_DIR$/generics/src/traits.rs" />
|
||||
<option value="$PROJECT_DIR$/generics/src/main.rs" />
|
||||
<option value="$PROJECT_DIR$/generics/src/point.rs" />
|
||||
<option value="$PROJECT_DIR$/generics/src/lib.rs" />
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
@ -88,11 +113,11 @@
|
||||
<annotation-enable>false</annotation-enable>
|
||||
<other-services-enabled>false</other-services-enabled>
|
||||
</component>
|
||||
<component name="ProjectFrameBounds">
|
||||
<option name="x" value="1920" />
|
||||
<option name="y" value="-438" />
|
||||
<option name="width" value="1080" />
|
||||
<option name="height" value="1897" />
|
||||
<component name="ProjectFrameBounds" extendedState="6">
|
||||
<option name="x" value="1680" />
|
||||
<option name="y" value="371" />
|
||||
<option name="width" value="1050" />
|
||||
<option name="height" value="876" />
|
||||
</component>
|
||||
<component name="ProjectLevelVcsManager" settingsEditedManually="true">
|
||||
<ConfirmationsSetting value="1" id="Add" />
|
||||
@ -112,7 +137,13 @@
|
||||
<path>
|
||||
<item name="rust" type="3d21c010:ScopeViewTreeModel$ProjectNode" />
|
||||
<item name="rust" type="2674bda8:ScopeViewTreeModel$GroupNode" />
|
||||
<item name="errors" type="9f88c78c:ScopeViewTreeModel$FileNode" />
|
||||
<item name="generics" 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="src" type="9f88c78c:ScopeViewTreeModel$FileNode" />
|
||||
</path>
|
||||
</expand>
|
||||
<select />
|
||||
@ -145,7 +176,7 @@
|
||||
</component>
|
||||
<component name="PropertiesComponent">
|
||||
<property name="JavaScriptWeakerCompletionTypeGuess" value="true" />
|
||||
<property name="com.android.tools.idea.instantapp.provision.ProvisionBeforeRunTaskProvider.myTimeStamp" value="1548883606455" />
|
||||
<property name="com.android.tools.idea.instantapp.provision.ProvisionBeforeRunTaskProvider.myTimeStamp" value="1548952830934" />
|
||||
<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" />
|
||||
@ -173,7 +204,7 @@
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
<component name="RunManager" selected="Cargo Command.Run errors">
|
||||
<component name="RunManager" selected="Cargo Command.Run generics">
|
||||
<configuration name="<template>" type="TestNG" default="true" selected="false">
|
||||
<option name="MAIN_CLASS_NAME" />
|
||||
<option name="VM_PARAMETERS" value="-ea" />
|
||||
@ -212,6 +243,26 @@
|
||||
<envs />
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
<configuration name="Run generics" type="CargoCommandRunConfiguration" factoryName="Cargo Command" temporary="true">
|
||||
<option name="channel" value="DEFAULT" />
|
||||
<option name="command" value="run --package generics --bin generics" />
|
||||
<option name="allFeatures" value="false" />
|
||||
<option name="nocapture" value="false" />
|
||||
<option name="backtrace" value="SHORT" />
|
||||
<option name="workingDirectory" value="file://$PROJECT_DIR$/generics" />
|
||||
<envs />
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
<configuration name="Run largest_number" type="CargoCommandRunConfiguration" factoryName="Cargo Command" temporary="true">
|
||||
<option name="channel" value="DEFAULT" />
|
||||
<option name="command" value="run --package largest_number --bin largest_number" />
|
||||
<option name="allFeatures" value="false" />
|
||||
<option name="nocapture" value="false" />
|
||||
<option name="backtrace" value="SHORT" />
|
||||
<option name="workingDirectory" value="file://$PROJECT_DIR$/largest_number/src" />
|
||||
<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" />
|
||||
@ -222,26 +273,6 @@
|
||||
<envs />
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
<configuration name="Run rectangles" type="CargoCommandRunConfiguration" factoryName="Cargo Command" temporary="true">
|
||||
<option name="channel" value="DEFAULT" />
|
||||
<option name="command" value="run --package rectangles --bin rectangles" />
|
||||
<option name="allFeatures" value="false" />
|
||||
<option name="nocapture" value="false" />
|
||||
<option name="backtrace" value="SHORT" />
|
||||
<option name="workingDirectory" value="file://$PROJECT_DIR$/rectangles" />
|
||||
<envs />
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
<configuration name="Run structs" type="CargoCommandRunConfiguration" factoryName="Cargo Command" temporary="true">
|
||||
<option name="channel" value="DEFAULT" />
|
||||
<option name="command" value="run --package structs --bin structs" />
|
||||
<option name="allFeatures" value="false" />
|
||||
<option name="nocapture" value="false" />
|
||||
<option name="backtrace" value="SHORT" />
|
||||
<option name="workingDirectory" value="file://$PROJECT_DIR$/structs" />
|
||||
<envs />
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
<configuration default="true" type="JUnit" factoryName="JUnit">
|
||||
<option name="TEST_OBJECT" value="class" />
|
||||
<option name="WORKING_DIRECTORY" value="$MODULE_DIR$" />
|
||||
@ -251,20 +282,20 @@
|
||||
</configuration>
|
||||
<recent_temporary>
|
||||
<list>
|
||||
<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" />
|
||||
<item itemvalue="Cargo Command.Run rectangles" />
|
||||
<item itemvalue="Cargo Command.Run structs" />
|
||||
</list>
|
||||
</recent_temporary>
|
||||
</component>
|
||||
<component name="ToolWindowManager">
|
||||
<frame x="1920" y="-438" width="1080" height="1897" extended-state="0" />
|
||||
<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.4970809" visible="true" weight="0.2540132" />
|
||||
<window_info id="Structure" order="1" sideWeight="0.5029191" side_tool="true" visible="true" weight="0.2540132" />
|
||||
<window_info content_ui="combo" id="Project" order="0" sideWeight="0.49724266" visible="true" weight="0.26141885" />
|
||||
<window_info id="Structure" order="1" sideWeight="0.5027574" 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" />
|
||||
@ -272,7 +303,7 @@
|
||||
<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="Find" order="1" weight="0.32979318" />
|
||||
<window_info anchor="bottom" id="Run" order="2" visible="true" weight="0.32979318" />
|
||||
<window_info active="true" anchor="bottom" id="Run" order="2" visible="true" weight="0.29761136" />
|
||||
<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="Inspection" order="5" weight="0.4" />
|
||||
@ -280,7 +311,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" weight="0.32979318" />
|
||||
<window_info anchor="bottom" id="Terminal" order="10" weight="0.32924467" />
|
||||
<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" />
|
||||
@ -454,6 +485,13 @@
|
||||
<entry file="file://$PROJECT_DIR$/errors/hello.txt">
|
||||
<provider selected="true" editor-type-id="text-editor" />
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/.gitignore">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="720">
|
||||
<caret line="45" column="16" selection-start-line="45" selection-start-column="16" selection-end-line="45" selection-end-column="16" />
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/errors/src/main.rs">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="1024">
|
||||
@ -465,10 +503,45 @@
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/.gitignore">
|
||||
<entry file="file://$PROJECT_DIR$/generics/Cargo.toml">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="720">
|
||||
<caret line="45" column="16" selection-start-line="45" selection-start-column="16" selection-end-line="45" selection-end-column="16" />
|
||||
<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$/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>
|
||||
<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>
|
||||
<entry file="file://$PROJECT_DIR$/generics/src/lib.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" />
|
||||
<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>
|
||||
|
7
generics/Cargo.toml
Normal file
7
generics/Cargo.toml
Normal file
@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "generics"
|
||||
version = "0.1.0"
|
||||
authors = ["Timothy Warren <twarren@nabancard.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
80
generics/src/lib.rs
Normal file
80
generics/src/lib.rs
Normal file
@ -0,0 +1,80 @@
|
||||
use std::fmt::Display;
|
||||
|
||||
struct Pair<T> {
|
||||
x: T,
|
||||
y: T,
|
||||
}
|
||||
|
||||
impl<T> Pair<T> {
|
||||
fn new(x: T, y: T) -> Self {
|
||||
Self {
|
||||
x,
|
||||
y,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Only implements method if both traits are satisfied
|
||||
impl<T: Display + PartialOrd> Pair<T> {
|
||||
fn cmp_display(&self) {
|
||||
if self.x >= self.y {
|
||||
println!("The largest member is x = {}", self.x);
|
||||
} else {
|
||||
println!("The largest member is y = {}", self.y);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Summary {
|
||||
fn summarize_author(&self) -> String;
|
||||
|
||||
// default implementation
|
||||
fn summarize(&self) -> String {
|
||||
String::from("(Read more...)")
|
||||
}
|
||||
}
|
||||
|
||||
pub struct NewsArticle {
|
||||
pub headline: String,
|
||||
pub location: String,
|
||||
pub author: String,
|
||||
pub content: String,
|
||||
}
|
||||
|
||||
impl Summary for NewsArticle {
|
||||
fn summarize_author(&self) -> String {
|
||||
self.author.to_string()
|
||||
}
|
||||
|
||||
fn summarize(&self) -> String {
|
||||
format!("{}, by {} ({})", self.headline, self.author, self.location)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Tweet {
|
||||
pub username: String,
|
||||
pub content: String,
|
||||
pub reply: bool,
|
||||
pub retweet: bool,
|
||||
}
|
||||
|
||||
// Uses default implementation for summarize method
|
||||
impl Summary for Tweet {
|
||||
fn summarize_author(&self) -> String {
|
||||
format!("@{}", self.username)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn returns_summarizable() -> impl Summary {
|
||||
Tweet {
|
||||
username: String::from("horse_ebooks"),
|
||||
content: String::from("of course, as you probably already know, people"),
|
||||
reply: false,
|
||||
retweet: false,
|
||||
}
|
||||
}
|
||||
|
||||
// Function/method requiring a trait's implementation
|
||||
// pub fn notify(item: impl Summary) {
|
||||
// println!("Breaking news! {}", item.summarize());
|
||||
// }
|
40
generics/src/main.rs
Normal file
40
generics/src/main.rs
Normal file
@ -0,0 +1,40 @@
|
||||
mod lib;
|
||||
mod point;
|
||||
|
||||
use point::{Point, DoubleGenericPoint};
|
||||
|
||||
// Works on types implementing both the PartialOrd and Copy traits
|
||||
fn largest<T: PartialOrd + Copy>(list: &[T]) -> T {
|
||||
let mut largest = list[0];
|
||||
|
||||
for &item in list.iter() {
|
||||
if item > largest {
|
||||
largest = item;
|
||||
}
|
||||
}
|
||||
|
||||
largest
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let number_list = vec![34, 50, 25, 100, 65];
|
||||
|
||||
let result = largest(&number_list);
|
||||
println!("The largest number is {}", result);
|
||||
|
||||
let char_list = vec!['y', 'm', 'a', 'q'];
|
||||
|
||||
let result = largest(&char_list);
|
||||
println!("The largest char is {}", result);
|
||||
|
||||
let p = Point { x: 5, y: 10 };
|
||||
|
||||
println!("p.x = {}", p.x());
|
||||
|
||||
let p1 = DoubleGenericPoint { x: 5, y: 10.4 };
|
||||
let p2 = DoubleGenericPoint { x: "Hello", y: 'c'};
|
||||
|
||||
let p3 = p1.mixup(p2);
|
||||
|
||||
println!("p3.x = {}, p3.y = {}", p3.x, p3.y);
|
||||
}
|
34
generics/src/point.rs
Normal file
34
generics/src/point.rs
Normal file
@ -0,0 +1,34 @@
|
||||
// Double generic types for a struct
|
||||
pub struct DoubleGenericPoint<T, U> {
|
||||
pub x: T,
|
||||
pub y: U,
|
||||
}
|
||||
|
||||
impl<T,U> DoubleGenericPoint<T, U> {
|
||||
// Takes a generic of potentially different types than the parent struct
|
||||
pub fn mixup<V, W>(self, other: DoubleGenericPoint<V,W>) -> DoubleGenericPoint<T, W> {
|
||||
DoubleGenericPoint {
|
||||
x: self.x,
|
||||
y: other.y
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Point<T> {
|
||||
pub x: T,
|
||||
pub y: T,
|
||||
}
|
||||
|
||||
// Implement generic methods on a generic struct
|
||||
impl<T> Point<T> {
|
||||
pub fn x(&self) -> &T {
|
||||
&self.x
|
||||
}
|
||||
}
|
||||
|
||||
// Implement a method for only a specific type of generic
|
||||
impl Point<f32> {
|
||||
fn distance_from_origin(&self) -> f32 {
|
||||
(self.x.powi(2) + self.y.powi(2)).sqrt()
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user