Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
R
rust-tps
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
Mamadu-lamarana Bah
rust-tps
Commits
258a20a8
Commit
258a20a8
authored
2 months ago
by
Bah Mamadu-lamarana
Browse files
Options
Downloads
Patches
Plain Diff
tp folder
parent
66e159ef
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
tp1/Cargo.toml
+6
-0
6 additions, 0 deletions
tp1/Cargo.toml
tp1/src/main.rs
+130
-0
130 additions, 0 deletions
tp1/src/main.rs
with
136 additions
and
0 deletions
tp1/Cargo.toml
0 → 100644
+
6
−
0
View file @
258a20a8
[package]
name
=
"tp1"
version
=
"0.1.0"
edition
=
"2024"
[dependencies]
This diff is collapsed.
Click to expand it.
tp1/src/main.rs
0 → 100644
+
130
−
0
View file @
258a20a8
use
std
::{
io
,
net
::
IpAddr
};
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
,
}
impl
PasswordChecker
{
fn
new
(
pass
:
&
str
)
->
Self
{
Self
{
secret
:
pass
.to_string
()
}
}
pub
fn
check
(
&
self
,
input
:
&
str
)
->
Result
<
(),
PasswordError
>
{
if
input
.trim
()
==
self
.secret
{
Ok
(())
}
else
{
if
input
.trim
()
.is_empty
()
{
Err
(
PasswordError
::
Empty
)
}
else
{
Err
(
PasswordError
::
Wrong
)
}
}
}
}
fn
main
()
{
println!
(
"Hello, world!"
);
// let x: i8 = 42;
// println!("{x}");
// eprintln!("{x:#08x}");
// let mut y = 1;
// while y < 11 {
// println!("{y}");
// y += 1;
// }
// check_password();
println!
(
"Enter password: "
);
let
mut
input
=
String
::
new
();
let
_
=
io
::
stdin
()
.read_line
(
&
mut
input
);
let
checker
=
PasswordChecker
::
new
(
"Rust"
);
// Call the check function on the checker.
// This function should have the same return type as
// the check_password function of the previous exercise
match
checker
.check
(
&
input
)
{
Ok
(())
=>
println!
(
"ok"
),
Err
(
PasswordError
::
Empty
)
=>
println!
(
"Empty Password"
),
Err
(
PasswordError
::
Wrong
)
=>
println!
(
"Wrong password"
),
}
}
/// Reads a file that contains an address per line and returns a Vector with all well-formed
/// addresses and prints a warning on the standard error output for every malformed lines.
///
/// Addresses should be ipv4 or ipv6 addresses.
fn
get_addrs
(
path
:
&
str
)
->
Result
<
Vec
<
IpAddr
>
,
std
::
io
::
Error
>
{
todo!
{}
}
/// Tries to connect to a given host with a TCP Connection with a one second timeout.
///
/// Returns Ok(()) or a corresponding error if connection fails.
fn
try_connect
(
addr
:
IpAddr
,
port
:
u16
)
->
Result
<
(),
std
::
io
::
Error
>
{
todo!
()
}
// fn check_password() {
// println!("Enter password: ");
// let mut input = String::new();
// let _ = io::stdin().read_line(&mut input);
// match check_password3(&input) {
// Ok(()) => println!("ok"),
// Err(PasswordError::Empty) => println!("Empty Password"),
// Err(PasswordError::Wrong) => println!("Wrong password"),
// }
// }
// Check an input for a predetermined secret password
// Returns `Ok(())` if the input matches the password or `Err(())` otherwise.
// pub fn check_password2(input: &str) -> Result<(), ()> {
// let predefined_password = "Rust";
// if input.trim() == predefined_password {
// // println!("Good");
// Ok(())
// } else {
// // println!("Wrong");
// Err(())
// }
// }
// pub fn check_password3(input: &str) -> Result<(), PasswordError> {
// let predefined_password = "Rust";
// if input.trim() == predefined_password {
// Ok(())
// } else {
// if input.trim().is_empty() {
// Err(PasswordError::Empty)
// }else {
// Err(PasswordError::Wrong)
// }
// }
// }
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