Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
R
rust-101
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
tom.andrivet
rust-101
Commits
ebcd5f89
Commit
ebcd5f89
authored
1 year ago
by
orestis.malaspin
Committed by
Michaël El Kharroubi
1 year ago
Browse files
Options
Downloads
Patches
Plain Diff
Adds part03 traits and genericity
parent
2a7c5bc3
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
codes/rust_lang/part03/Cargo.toml
+8
-0
8 additions, 0 deletions
codes/rust_lang/part03/Cargo.toml
codes/rust_lang/part03/src/main.rs
+103
-0
103 additions, 0 deletions
codes/rust_lang/part03/src/main.rs
with
111 additions
and
0 deletions
codes/rust_lang/part03/Cargo.toml
0 → 100644
+
8
−
0
View file @
ebcd5f89
[package]
name
=
"part03"
version
=
"0.1.0"
edition
=
"2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
This diff is collapsed.
Click to expand it.
codes/rust_lang/part03/src/main.rs
0 → 100644
+
103
−
0
View file @
ebcd5f89
/// In part03 we introduce genericity through traits and in particular, [Copy],
/// [Clone], [std::fmt::Display] .
enum
SomethingOrNothing
<
T
>
{
Nothing
,
Something
(
T
),
}
// We know the generic type T must be Displayable
impl
<
T
:
std
::
fmt
::
Display
>
SomethingOrNothing
<
T
>
{
// Static function
fn
print
(
self
)
{
match
self
{
SomethingOrNothing
::
Nothing
=>
println!
(
"Nothing."
),
SomethingOrNothing
::
Something
(
val
)
=>
println!
(
"Something is: {}"
,
val
),
}
}
}
impl
<
T
:
Clone
>
Clone
for
SomethingOrNothing
<
T
>
{
fn
clone
(
&
self
)
->
Self
{
match
self
{
SomethingOrNothing
::
Nothing
=>
SomethingOrNothing
::
Nothing
,
SomethingOrNothing
::
Something
(
val
)
=>
SomethingOrNothing
::
Something
(
val
.clone
()),
}
}
}
impl
<
T
:
Copy
>
Copy
for
SomethingOrNothing
<
T
>
{}
// If we remove Copy, we have a problem with the t in tab
// in the computation of the minimum.
trait
Minimum
:
Copy
{
fn
min
(
self
,
rhs
:
Self
)
->
Self
;
}
impl
<
T
:
Minimum
>
Minimum
for
SomethingOrNothing
<
T
>
{
fn
min
(
self
,
rhs
:
Self
)
->
Self
{
match
(
self
,
rhs
)
{
(
SomethingOrNothing
::
Nothing
,
SomethingOrNothing
::
Nothing
)
=>
{
SomethingOrNothing
::
Nothing
}
(
SomethingOrNothing
::
Something
(
lhs
),
SomethingOrNothing
::
Something
(
rhs
))
=>
{
SomethingOrNothing
::
Something
(
lhs
.min
(
rhs
))
}
(
SomethingOrNothing
::
Nothing
,
SomethingOrNothing
::
Something
(
rhs
))
=>
{
SomethingOrNothing
::
Something
(
rhs
)
}
(
SomethingOrNothing
::
Something
(
lhs
),
SomethingOrNothing
::
Nothing
)
=>
{
SomethingOrNothing
::
Something
(
lhs
)
}
}
}
}
// i32 is Copyable as a very basic type as f32, f64, etc.
// Arrays for example are not copyable.
impl
Minimum
for
i32
{
fn
min
(
self
,
rhs
:
Self
)
->
Self
{
if
self
<
rhs
{
self
}
else
{
rhs
}
}
}
const
SIZE
:
usize
=
9
;
fn
read_command_line
()
->
[
i32
;
SIZE
]
{
[
10
,
32
,
12
,
43
,
52
,
53
,
83
,
2
,
9
]
}
// Prints tab and returns tab.
// Tab would be destructed at the end of the function otherwise.
fn
print_tab
(
tab
:
[
i32
;
SIZE
])
->
[
i32
;
SIZE
]
{
for
t
in
tab
{
print!
(
"{} "
,
t
);
}
println!
();
tab
}
fn
find_min
<
T
:
Minimum
>
(
tab
:
[
T
;
SIZE
])
->
([
T
;
SIZE
],
SomethingOrNothing
<
T
>
)
{
let
mut
min
=
SomethingOrNothing
::
Nothing
;
// Here is T is not Copyable tab is consumed and cannot be returned
for
t
in
tab
{
min
=
min
.min
(
SomethingOrNothing
::
Something
(
t
));
}
(
tab
,
min
)
}
fn
main
()
{
let
tab
=
read_command_line
();
println!
(
"Among the Somethings in the list:"
);
let
tab
=
print_tab
(
tab
);
// There are alternatives to access fields of tuples
let
(
_
,
min
)
=
find_min
(
tab
);
// The first field is not used therefore we can replace it with "_"
min
.print
();
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment