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
8e661fdb
Commit
8e661fdb
authored
1 year ago
by
orestis.malaspin
Committed by
Michaël El Kharroubi
1 year ago
Browse files
Options
Downloads
Patches
Plain Diff
Adds part02 with enum and pattern matching
parent
858ec252
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
codes/rust_lang/part01/src/main.rs
+8
-8
8 additions, 8 deletions
codes/rust_lang/part01/src/main.rs
codes/rust_lang/part02/Cargo.toml
+8
-0
8 additions, 0 deletions
codes/rust_lang/part02/Cargo.toml
codes/rust_lang/part02/src/main.rs
+68
-0
68 additions, 0 deletions
codes/rust_lang/part02/src/main.rs
with
84 additions
and
8 deletions
codes/rust_lang/part01/src/main.rs
+
8
−
8
View file @
8e661fdb
...
@@ -2,6 +2,7 @@
...
@@ -2,6 +2,7 @@
/// - Functions
/// - Functions
/// - Arguments are "moved"
/// - Arguments are "moved"
/// - if is an expression
/// - if is an expression
/// - for loops revisited
/// - Tuples
/// - Tuples
/// - Destructuring
/// - Destructuring
...
@@ -20,10 +21,9 @@ fn check_size(size: usize) {
...
@@ -20,10 +21,9 @@ fn check_size(size: usize) {
// Prints tab and returns tab.
// Prints tab and returns tab.
// Tab would be destructed at the end of the function otherwise.
// Tab would be destructed at the end of the function otherwise.
fn
print_tab
(
tab
:
[
i32
;
SIZE
],
size
:
usize
)
->
[
i32
;
SIZE
]
{
fn
print_tab
(
tab
:
[
i32
;
SIZE
])
->
[
i32
;
SIZE
]
{
check_size
(
size
);
for
t
in
tab
{
for
i
in
0
..
size
{
print!
(
"{} "
,
t
);
print!
(
"{} "
,
tab
[
i
]);
}
}
println!
();
println!
();
tab
tab
...
@@ -39,9 +39,9 @@ fn min_i32(lhs: i32, rhs: i32) -> i32 {
...
@@ -39,9 +39,9 @@ fn min_i32(lhs: i32, rhs: i32) -> i32 {
fn
find_min
(
tab
:
[
i32
;
SIZE
],
size
:
usize
)
->
([
i32
;
SIZE
],
i32
)
{
fn
find_min
(
tab
:
[
i32
;
SIZE
],
size
:
usize
)
->
([
i32
;
SIZE
],
i32
)
{
check_size
(
size
);
check_size
(
size
);
let
mut
min
=
tab
[
0
]
;
let
mut
min
=
i32
::
MAX
;
for
i
in
1
..
size
{
for
t
in
tab
{
min
=
min_i32
(
min
,
t
ab
[
i
]
);
min
=
min_i32
(
min
,
t
);
}
}
(
tab
,
min
)
(
tab
,
min
)
}
}
...
@@ -50,7 +50,7 @@ fn main() {
...
@@ -50,7 +50,7 @@ fn main() {
let
tab
=
read_command_line
();
let
tab
=
read_command_line
();
println!
(
"Among the numbers in the list:"
);
println!
(
"Among the numbers in the list:"
);
let
tab
=
print_tab
(
tab
,
SIZE
);
let
tab
=
print_tab
(
tab
);
// There are alternatives to access fields of tuples
// There are alternatives to access fields of tuples
let
(
_
,
min
)
=
find_min
(
tab
,
SIZE
);
let
(
_
,
min
)
=
find_min
(
tab
,
SIZE
);
...
...
This diff is collapsed.
Click to expand it.
codes/rust_lang/part02/Cargo.toml
0 → 100644
+
8
−
0
View file @
8e661fdb
[package]
name
=
"part02"
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/part02/src/main.rs
0 → 100644
+
68
−
0
View file @
8e661fdb
/// In part03 we introduce `Enums` (also known as `Algebraic Data Types`), `Pattern Matching`,
/// and `Static Functions`.
enum
NumberOrNothing
{
Nothing
,
Number
(
i32
),
}
impl
NumberOrNothing
{
fn
new
(
val
:
i32
)
->
Self
{
NumberOrNothing
::
Number
(
val
)
}
// Static function
fn
print
(
self
)
{
match
self
{
NumberOrNothing
::
Nothing
=>
println!
(
"No number."
),
NumberOrNothing
::
Number
(
val
)
=>
println!
(
"The number is: {}"
,
val
),
}
}
}
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
min_i32
(
lhs
:
i32
,
rhs
:
i32
)
->
i32
{
if
lhs
<
rhs
{
lhs
}
else
{
rhs
}
}
fn
find_min
(
tab
:
[
i32
;
SIZE
])
->
([
i32
;
SIZE
],
NumberOrNothing
)
{
let
mut
min
=
NumberOrNothing
::
Nothing
;
for
t
in
tab
{
match
min
{
NumberOrNothing
::
Nothing
=>
min
=
NumberOrNothing
::
new
(
t
),
NumberOrNothing
::
Number
(
val
)
=>
min
=
NumberOrNothing
::
new
(
min_i32
(
val
,
t
)),
}
}
(
tab
,
min
)
}
fn
main
()
{
let
tab
=
read_command_line
();
println!
(
"Among the numbers 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