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
1079c46b
Commit
1079c46b
authored
1 year ago
by
orestis.malaspin
Browse files
Options
Downloads
Plain Diff
Merge branch 'typos' into 'main'
Removed some typos See merge request
orestis.malaspin/rust-101!23
parents
8c1f7553
105454f8
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
codes/rust_lang/part08/src/big_int.rs
+0
-117
0 additions, 117 deletions
codes/rust_lang/part08/src/big_int.rs
codes/rust_lang/part08/src/main.rs
+1
-1
1 addition, 1 deletion
codes/rust_lang/part08/src/main.rs
with
1 addition
and
118 deletions
codes/rust_lang/part08/src/big_int.rs
deleted
100644 → 0
+
0
−
117
View file @
8c1f7553
use
crate
::
minimum
::
Minimum
;
/// Larger ints based on a [Vec] of [u8] to repensent arbitrary lengthy numbers.
/// The number has a sign as well.
pub
struct
BigInt
{
/// The data contains the unsigned integers that are read from right to left
/// The number 1337 is stored as vec![7, 3, 3, 1]. Each number must be in the range [0,9]
/// and no trailing 0s are allowed.
data
:
Vec
<
u8
>
,
/// Contains the sign of the number +/-1;
sign
:
i8
,
}
impl
BigInt
{
/// Tries to create a new [BigInt]. If the number is valid it returns
/// an Ok(BigInt) an Error otherwise.
///
/// # Examples
///
/// ```
/// use part08::big_int::BigInt;
/// let num = BigInt::try_new(vec![1, 2, 3, 4], 1);
/// assert!(num.is_ok());
/// let num = BigInt::try_new(vec![1, 2, 3, 4], -1);
/// assert!(num.is_ok());
/// let num = BigInt::try_new(vec![1, 2, 3, 4], 10);
/// assert!(num.is_err());
/// let num = BigInt::try_new(vec![1, 2, 3, 4], -10);
/// assert!(num.is_err());
/// ```
///
pub
fn
try_new
(
data
:
Vec
<
u8
>
,
sign
:
i8
)
->
Result
<
Self
,
String
>
{
// EXERCISE:
// We don't check for trailing 0s but maybe this could be an exercise.
// Also we should check numbers are between 0 and 9.
if
sign
==
1
||
sign
==
-
1
{
Ok
(
BigInt
{
data
,
sign
})
}
else
{
Err
(
String
::
from
(
"Invalid sign."
))
}
}
}
impl
Clone
for
BigInt
{
fn
clone
(
&
self
)
->
Self
{
BigInt
{
data
:
self
.data
.clone
(),
sign
:
self
.sign
,
}
}
}
impl
Minimum
for
BigInt
{
// EXERCISE: Correct this function by using clippy and let it guide you.
// Get inspiration from Display to compute the Minimum
fn
min
(
self
,
rhs
:
Self
)
->
Self
{
if
self
.sign
<
rhs
.sign
{
return
self
;
}
else
if
self
.sign
>
rhs
.sign
{
return
rhs
;
}
if
self
.data
.len
()
<
rhs
.data
.len
()
{
return
self
;
}
else
if
self
.data
.len
()
>
rhs
.data
.len
()
{
return
rhs
;
}
for
(
l
,
r
)
in
self
.data
.iter
()
.rev
()
.zip
(
rhs
.data
.iter
()
.rev
())
{
let
ls
=
(
*
l
as
i8
)
*
self
.sign
;
let
rs
=
(
*
r
as
i8
)
*
self
.sign
;
if
ls
<
rs
{
return
self
;
}
else
if
ls
>
rs
{
return
rhs
;
}
}
self
}
}
impl
PartialEq
for
BigInt
{
fn
eq
(
&
self
,
other
:
&
Self
)
->
bool
{
if
self
.sign
==
other
.sign
&&
self
.data
.len
()
==
other
.data
.len
()
{
self
.data
.iter
()
.zip
(
other
.data
.iter
())
.try_fold
(
true
,
|
_
,
(
l
,
r
)|
if
l
==
r
{
Some
(
true
)
}
else
{
None
})
.is_some
()
}
else
{
false
}
}
}
impl
std
::
fmt
::
Display
for
BigInt
{
fn
fmt
(
&
self
,
f
:
&
mut
std
::
fmt
::
Formatter
<
'_
>
)
->
std
::
fmt
::
Result
{
// This could be replaced by an `?`
if
self
.sign
==
-
1
{
if
let
Err
(
e
)
=
write!
(
f
,
"-"
)
{
return
Err
(
e
);
}
}
// This could be replaced by an `?`
let
res
=
self
.data
.iter
()
.rev
()
.try_fold
((),
|
_
,
t
|
write!
(
f
,
"{}"
,
t
));
res
}
}
// EXERCISE: write tests
// EXERCISE: Modify Minimum trait to take references?
#[cfg(test)]
mod
tests
{}
This diff is collapsed.
Click to expand it.
codes/rust_lang/part08/src/main.rs
+
1
−
1
View file @
1079c46b
...
@@ -6,7 +6,7 @@ fn main() -> Result<(), String> {
...
@@ -6,7 +6,7 @@ fn main() -> Result<(), String> {
Ok
(
tab
)
=>
tab
,
Ok
(
tab
)
=>
tab
,
Err
(
s
)
=>
return
Err
(
s
),
Err
(
s
)
=>
return
Err
(
s
),
};
};
println!
(
"Among the
Big I
nts in the list:"
);
println!
(
"Among the
custom i
nts in the list:"
);
io
::
print_tab_CustomInt
(
&
tab
);
io
::
print_tab_CustomInt
(
&
tab
);
// There are alternatives to access fields of tuples
// There are alternatives to access fields of tuples
let
min
=
find_min
(
&
tab
);
let
min
=
find_min
(
&
tab
);
...
...
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