Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
destinyFvcker committed Apr 11, 2024
1 parent 088770c commit cd54261
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 22 deletions.
19 changes: 13 additions & 6 deletions exercises/conversions/as_ref_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,32 @@
// Execute `rustlings hint as_ref_mut` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

// Obtain the number of bytes (not characters) in the given argument.
// TODO: Add the AsRef trait appropriately as a trait bound.
fn byte_counter<T>(arg: T) -> usize {
fn byte_counter<T>(arg: T) -> usize
where
T: AsRef<str>,
{
arg.as_ref().as_bytes().len()
}

// Obtain the number of characters (not bytes) in the given argument.
// TODO: Add the AsRef trait appropriately as a trait bound.
fn char_counter<T>(arg: T) -> usize {
fn char_counter<T>(arg: T) -> usize
where
T: AsRef<str>,
{
arg.as_ref().chars().count()
}

// Squares a number using as_mut().
// TODO: Add the appropriate trait bound.
fn num_sq<T>(arg: &mut T) {
fn num_sq<T>(arg: &mut T)
where
T: AsMut<u32>,
{
// TODO: Implement the function body.
???
*arg.as_mut() *= *arg.as_mut()
}

#[cfg(test)]
Expand Down
42 changes: 40 additions & 2 deletions exercises/conversions/try_from_into.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ enum IntoColorError {
IntConversion,
}

// I AM NOT DONE

// Your task is to complete this implementation and return an Ok result of inner
// type Color. You need to create an implementation for a tuple of three
// integers, an array of three integers, and a slice of integers.
Expand All @@ -41,20 +39,60 @@ enum IntoColorError {
impl TryFrom<(i16, i16, i16)> for Color {
type Error = IntoColorError;
fn try_from(tuple: (i16, i16, i16)) -> Result<Self, Self::Error> {
if tuple.0 < 0 || tuple.0 > 255 {
return Err(IntoColorError::IntConversion);
}
if tuple.1 < 0 || tuple.1 > 255 {
return Err(IntoColorError::IntConversion);
}
if tuple.2 < 0 || tuple.2 > 255 {
return Err(IntoColorError::IntConversion);
}

Ok(Color {
red: tuple.0 as u8,
green: tuple.1 as u8,
blue: tuple.2 as u8,
})
}
}

// Array implementation
impl TryFrom<[i16; 3]> for Color {
type Error = IntoColorError;
fn try_from(arr: [i16; 3]) -> Result<Self, Self::Error> {
for item in arr {
if item < 0 || item > 255 {
return Err(IntoColorError::IntConversion);
}
}

Ok(Color {
red: arr[0] as u8,
green: arr[1] as u8,
blue: arr[2] as u8,
})
}
}

// Slice implementation
impl TryFrom<&[i16]> for Color {
type Error = IntoColorError;
fn try_from(slice: &[i16]) -> Result<Self, Self::Error> {
if slice.len() != 3 {
return Err(IntoColorError::BadLen);
}
for item in slice {
if *item < 0 || *item > 255 {
return Err(IntoColorError::IntConversion);
}
}

Ok(Color {
red: slice[0] as u8,
green: slice[1] as u8,
blue: slice[2] as u8,
})
}
}

Expand Down
7 changes: 7 additions & 0 deletions exercises/tests/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions exercises/tests/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "tests8"
version = "0.0.1"
edition = "2021"
[[bin]]
name = "tests8"
path = "tests8.rs"
12 changes: 5 additions & 7 deletions exercises/tests/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@ fn main() {
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_secs(); // What's the use of this timestamp here?
let your_command = format!(
"Your command here with {}, please checkout exercises/tests/build.rs",
timestamp
);
println!("cargo:{}", your_command);
let your_command = format!("rustc-env=TEST_FOO={}", timestamp);
println!("cargo::{}", your_command);
// println!("cargo::rustc-env=TEST_FOO=destinyFvcker");

// In tests8, we should enable "pass" feature to make the
// testcase return early. Fill in the command to tell
// Cargo about that.
let your_command = "Your command here, please checkout exercises/tests/build.rs";
println!("cargo:{}", your_command);
let your_command = "rustc-cfg=feature=\"pass\"";
println!("cargo::{}", your_command);
}
9 changes: 6 additions & 3 deletions exercises/tests/tests5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,19 @@
// Execute `rustlings hint tests5` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

/// # Safety
///
/// The `address` must contain a mutable reference to a valid `u32` value.
unsafe fn modify_by_address(address: usize) {
// TODO: Fill your safety notice of the code block below to match your
// code's behavior and the contract of this function. You may use the
// comment of the test below as your format reference.
unsafe { todo!("Your code goes here") }
unsafe {
// let mut point = Box::from_raw(address as *mut u32);
// *point = 0xAABBCCDD;
let point: *mut u32 = address as *mut u32;
*point = 0xAABBCCDD;
}
}

#[cfg(test)]
Expand Down
2 changes: 0 additions & 2 deletions exercises/tests/tests7.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
// Execute `rustlings hint tests7` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

fn main() {}

#[cfg(test)]
Expand Down
2 changes: 0 additions & 2 deletions exercises/tests/tests8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
// Execute `rustlings hint tests8` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

fn main() {}

#[cfg(test)]
Expand Down

0 comments on commit cd54261

Please sign in to comment.