Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
D
D7050E_2020
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
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Wilma Krutrök
D7050E_2020
Commits
cfd6b987
Commit
cfd6b987
authored
Oct 10, 2020
by
Per Lindgren
Browse files
Options
Downloads
Patches
Plain Diff
examples
parent
27b7cf3b
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
examples/generics.rs
+13
-1
13 additions, 1 deletion
examples/generics.rs
examples/traits.rs
+46
-0
46 additions, 0 deletions
examples/traits.rs
with
59 additions
and
1 deletion
examples/generics.rs
+
13
−
1
View file @
cfd6b987
...
...
@@ -4,6 +4,8 @@ struct P<T> {
y
:
T
,
}
// We can implement functions for
// instances of our generic type
impl
P
<
u32
>
{
fn
double
(
&
mut
self
)
{
self
.x
+=
self
.x
;
...
...
@@ -11,6 +13,7 @@ impl P<u32> {
}
}
// Another implementation
impl
P
<
u64
>
{
fn
double
(
&
mut
self
)
{
self
.x
+=
self
.x
;
...
...
@@ -26,10 +29,19 @@ fn main() {
let
mut
p2
=
P
{
x
:
1u64
,
y
:
2
};
p2
.double
();
println!
(
"x {} y {}"
,
p2
.x
,
p2
.y
);
let
mut
p3
:
P
<
u8
>
=
P
{
x
:
1u8
,
y
:
2
};
// p3.double(); // we don't have an implementation (yet)
println!
(
"x {} y {}"
,
p3
.x
,
p3
.y
);
}
// The types of p1 and p2 is known at compile time
// (See that rust analy
ses
derives the types)
// (See that rust analy
zer
derives the types
.
)
//
// The compiler will "specialize" the call
// to double (as cheap as an ordinary call)
//
// We have a zero-cost abstraction.
//
// (We cannot do double on p3, as we don't have any
// implementation of double for P<u8>.)
This diff is collapsed.
Click to expand it.
examples/traits.rs
0 → 100644
+
46
−
0
View file @
cfd6b987
// a struct with generics
// a trait for double
use
std
::
fmt
::
Debug
;
#[derive(Debug)]
struct
P
<
T
>
{
x
:
T
,
y
:
T
,
}
// Declare an "interface", for double.
trait
Double
{
fn
double
(
&
mut
self
);
}
// Implement Double for some types.
impl
Double
for
P
<
u32
>
{
fn
double
(
&
mut
self
)
{
self
.x
+=
self
.x
;
self
.y
+=
self
.y
;
}
}
impl
Double
for
String
{
fn
double
(
&
mut
self
)
{
let
s
=
self
.clone
();
self
.push_str
(
&
s
);
}
}
fn
main
()
{
let
mut
p1
=
P
{
x
:
1u32
,
y
:
2
};
p1
.double
();
println!
(
"{:?}"
,
p1
);
let
mut
s
=
"String, "
.to_string
();
s
.double
();
println!
(
"{:?}"
,
s
);
}
// Types of p1 and s is known at compile time
// Even if we now use Traits the compiler can
// specialize the calls to double.
//
// We have a zero-cost abstraction.
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