Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
COA-VANDEWAETER
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
Fabio Vandewaeter
COA-VANDEWAETER
Commits
158196b4
Commit
158196b4
authored
3 months ago
by
Fabio Vandewaeter
Browse files
Options
Downloads
Patches
Plain Diff
save
parent
d21b83c7
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
README.md
+72
-4
72 additions, 4 deletions
README.md
tp1/src/main.rs
+29
-12
29 additions, 12 deletions
tp1/src/main.rs
with
101 additions
and
16 deletions
README.md
+
72
−
4
View file @
158196b4
...
...
@@ -54,7 +54,7 @@ fn main() {
if
input
.trim
()
==
password
{
println!
(
"Good"
);
}
else
{
println!
(
"Wrong"
);
e
println!
(
"Wrong"
);
}
}
```
...
...
@@ -64,19 +64,87 @@ fn main() {
### 2.2.1. Simple function
```
rs
/// Check an input for a predetermined secret password
///
/// Returns `Ok(())` if the input matches the password or `Err(())` otherwise.
pub
fn
check_password
(
input
:
&
str
)
->
Result
<
(),
()
>
{
let
password
=
"password"
;
if
input
.trim
()
==
password
{
Ok
(())
}
else
{
Err
(())
}
}
```
###
###
2.2.2. Pattern matching
```
rs
use
std
::
io
;
/// Check an input for a predetermined secret password
///
/// Returns `Ok(())` if the input matches the password or `Err(())` otherwise.
pub
fn
check_password
(
input
:
&
str
)
->
Result
<
(),
()
>
{
let
password
=
"password"
;
if
input
.trim
()
==
password
{
Ok
(())
}
else
{
Err
(())
}
}
fn
main
()
{
println!
(
"Enter password:"
);
let
mut
input
=
String
::
new
();
io
::
stdin
()
.read_line
(
&
mut
input
)
.expect
(
"Failed to read line"
);
match
check_password
(
&
input
)
{
Ok
(())
=>
println!
(
"Good"
),
Err
(())
=>
eprintln!
(
"Wrong"
),
}
}
```
###
###
2.2.3. Enumeration
```
rs
use
std
::
io
;
pub
enum
PasswordError
{
Empty
,
Wrong
}
/// Check an input for a predetermined secret password
///
/// Returns `Ok(())` if the input matches the password or `Err(())` otherwise.
pub
fn
check_password
(
input
:
&
str
)
->
Result
<
(),
PasswordError
>
{
let
password
=
"password"
;
let
trimmed_input
=
input
.trim
();
if
trimmed_input
.is_empty
(){
Err
(
PasswordError
::
Empty
)
}
else
if
trimmed_input
.trim
()
==
password
{
Ok
(())
}
else
{
Err
(
PasswordError
::
Wrong
)
}
}
fn
main
()
{
println!
(
"Enter password:"
);
let
mut
input
=
String
::
new
();
io
::
stdin
()
.read_line
(
&
mut
input
)
.expect
(
"Failed to read line"
);
match
check_password
(
&
input
)
{
Ok
(())
=>
println!
(
"Good"
),
Err
(
PasswordError
::
Wrong
)
=>
eprintln!
(
"Error: Wrong password"
),
Err
(
PasswordError
::
Empty
)
=>
eprintln!
(
"Error: password is empty"
)
}
}
```
###
###
2.2.4. Structured types
```
rs
```
...
...
This diff is collapsed.
Click to expand it.
tp1/src/main.rs
+
29
−
12
View file @
158196b4
use
std
::
io
;
pub
enum
PasswordError
{
Empty
,
Wrong
}
/// A type that can be used to check if a given input is a valid password
pub
struct
PasswordChecker
{
secret
:
String
,
}
/// Check an input for a predetermined secret password
///
/// Returns `Ok(())` if the input matches the password or `Err(())` otherwise.
pub
fn
check_password
(
input
:
&
str
)
->
Result
<
(),
()
>
{
let
password
=
"password"
;
if
input
.trim
()
==
password
{
Ok
(())
}
else
{
Err
(())
impl
PasswordChecker
{
pub
fn
new
(
secret
:
&
str
)
->
Self
{
Self
{
secret
:
secret
.to_string
()}
}
pub
fn
check
(
&
self
,
input
:
&
str
)
->
Result
<
(),
PasswordError
>
{
let
trimmed_input
=
input
.trim
();
if
trimmed_input
.is_empty
(){
Err
(
PasswordError
::
Empty
)
}
else
if
trimmed_input
.trim
()
==
self
.secret
{
Ok
(())
}
else
{
Err
(
PasswordError
::
Wrong
)
}
}
}
...
...
@@ -17,9 +31,12 @@ fn main() {
println!
(
"Enter password:"
);
let
mut
input
=
String
::
new
();
io
::
stdin
()
.read_line
(
&
mut
input
)
.expect
(
"Failed to read line"
);
let
checker
=
PasswordChecker
::
new
(
"SuperSecret"
);
match
check
_password
(
&
input
)
{
match
check
er
.check
(
&
input
)
{
Ok
(())
=>
println!
(
"Good"
),
Err
(())
=>
println!
(
"Wrong"
),
Err
(
PasswordError
::
Wrong
)
=>
eprintln!
(
"Error: Wrong password"
),
Err
(
PasswordError
::
Empty
)
=>
eprintln!
(
"Error: password is empty"
)
}
}
\ No newline at end of file
}
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