Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: refactored as_ref_mut.rs to as_ref.rs and as_mut1.rs and add as_mut2.rs #1306

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions exercises/conversions/as_mut1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// AsMut allows for cheap mutable reference-to-reference conversions.
// Read more about it at https://doc.rust-lang.org/std/convert/trait.AsMut.html.
// Execute `rustlings hint as_mut1` or use the `hint` watch subcommand for a hint.

// I AM NOT DONE

// Squares a number using as_mut().
// TODO: Add the appropriate trait bound.
fn num_sq<T>(arg: &mut T) {
// TODO: Implement the function's body.
???
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn mult_box() {
let mut num: Box<u32> = Box::new(3);
num_sq(&mut num);
assert_eq!(*num, 9);
}
}
41 changes: 41 additions & 0 deletions exercises/conversions/as_mut2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// AsMut allows for cheap reference-to-reference conversions.
// Read more about it at https://doc.rust-lang.org/std/convert/trait.AsMut.html.
//
// In conversions/as_mut1.rs, we implemented a function that would square a
// Box<u32> in-place using as_mut(). Now we're going to generalize the function
// to work with a Box containing any numeric type that supports multiplication
// and assignment.
//
// Execute `rustlings hint as_mut2` or use the `hint` watch subcommand for a hint.

// I AM NOT DONE

// Squares a number using as_mut().
// TODO: Add the appropriate trait bounds.
fn num_sq<T, U>(arg: &mut T)
where
T: ???,
U: ???,
{
// TODO: Implement the function's body.
???
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn mult_box_u32() {
let mut num: Box<u32> = Box::new(3);
num_sq(&mut num);
assert_eq!(*num, 9);
}

#[test]
fn mult_box_f32() {
let mut num: Box<f32> = Box::new(3.0);
num_sq(&mut num);
assert_eq!(*num, 9.0);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// AsRef and AsMut allow for cheap reference-to-reference conversions.
// Read more about them at https://doc.rust-lang.org/std/convert/trait.AsRef.html
// and https://doc.rust-lang.org/std/convert/trait.AsMut.html, respectively.
// Execute `rustlings hint as_ref_mut` or use the `hint` watch subcommand for a hint.
// AsRef allows for cheap reference-to-reference conversions.
// Read more about it at https://doc.rust-lang.org/std/convert/trait.AsRef.html
// Execute `rustlings hint as_ref` or use the `hint` watch subcommand for a hint.

// I AM NOT DONE

Expand All @@ -17,13 +16,6 @@ fn char_counter<T>(arg: T) -> usize {
arg.as_ref().chars().count()
}

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

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -51,11 +43,4 @@ mod tests {
let s = String::from("Cafe au lait");
assert_eq!(char_counter(s.clone()), byte_counter(s));
}

#[test]
fn mult_box() {
let mut num: Box<u32> = Box::new(3);
num_sq(&mut num);
assert_eq!(*num, 9);
}
}
23 changes: 21 additions & 2 deletions info.toml
Original file line number Diff line number Diff line change
Expand Up @@ -1154,8 +1154,27 @@ https://doc.rust-lang.org/stable/rust-by-example/error/multiple_error_types/reen
Challenge: Can you make the `TryFrom` implementations generic over many integer types?"""

[[exercises]]
name = "as_ref_mut"
path = "exercises/conversions/as_ref_mut.rs"
name = "as_ref"
path = "exercises/conversions/as_ref.rs"
mode = "test"
hint = """
Add AsRef<str> as a trait bound to the functions."""

[[exercises]]
name = "as_mut1"
path = "exercises/conversions/as_mut1.rs"
mode = "test"
hint = """
Add AsMut<u32> as a trait bound to the function."""

[[exercises]]
name = "as_mut2"
path = "exercises/conversions/as_mut2.rs"
mode = "test"
hint = """
Now we need to tell the compiler about two types. Type T is Box<U>, while
type U is a number. Numbers can implement std::ops::MulAssign (more at
https://doc.rust-lang.org/std/ops/trait.MulAssign.html). The number
might also need to be copyable.

You might need to use a temporary variable in the function's body."""