Skip to content

Commit 4f96f3d

Browse files
committed
3
1 parent 26c47d1 commit 4f96f3d

File tree

9 files changed

+65
-26
lines changed

9 files changed

+65
-26
lines changed

exercises/conversions/as_ref_mut.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,23 @@
77
// Execute `rustlings hint as_ref_mut` or use the `hint` watch subcommand for a
88
// hint.
99

10-
// I AM NOT DONE
11-
1210
// Obtain the number of bytes (not characters) in the given argument.
1311
// TODO: Add the AsRef trait appropriately as a trait bound.
14-
fn byte_counter<T>(arg: T) -> usize {
12+
fn byte_counter<T: AsRef<str>>(arg: T) -> usize {
1513
arg.as_ref().as_bytes().len()
1614
}
1715

1816
// Obtain the number of characters (not bytes) in the given argument.
1917
// TODO: Add the AsRef trait appropriately as a trait bound.
20-
fn char_counter<T>(arg: T) -> usize {
18+
fn char_counter<T: AsRef<str>>(arg: T) -> usize {
2119
arg.as_ref().chars().count()
2220
}
2321

2422
// Squares a number using as_mut().
2523
// TODO: Add the appropriate trait bound.
26-
fn num_sq<T>(arg: &mut T) {
24+
fn num_sq<T: AsMut<u32>>(arg: &mut T) {
2725
// TODO: Implement the function body.
28-
???
26+
*arg.as_mut() = (*arg.as_mut()).pow(2);
2927
}
3028

3129
#[cfg(test)]

exercises/tests/Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

exercises/tests/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "tests8"
3+
version = "0.0.1"
4+
edition = "2021"
5+
[[bin]]
6+
name = "tests8"
7+
path = "tests8.rs"

exercises/tests/build.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,12 @@ fn main() {
1010
.duration_since(std::time::UNIX_EPOCH)
1111
.unwrap()
1212
.as_secs(); // What's the use of this timestamp here?
13-
let your_command = format!(
14-
"Your command here with {}, please checkout exercises/tests/build.rs",
15-
timestamp
16-
);
13+
let your_command = format!("rustc-env=TEST_FOO={}", timestamp);
1714
println!("cargo:{}", your_command);
1815

1916
// In tests8, we should enable "pass" feature to make the
2017
// testcase return early. Fill in the command to tell
2118
// Cargo about that.
22-
let your_command = "Your command here, please checkout exercises/tests/build.rs";
19+
let your_command = "rustc-cfg=feature=\"pass\"";
2320
println!("cargo:{}", your_command);
24-
}
21+
}

exercises/tests/tests5.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
// Execute `rustlings hint tests5` or use the `hint` watch subcommand for a
2323
// hint.
2424

25-
// I AM NOT DONE
26-
2725
/// # Safety
2826
///
2927
/// The `address` must contain a mutable reference to a valid `u32` value.
@@ -32,7 +30,7 @@ unsafe fn modify_by_address(address: usize) {
3230
// code's behavior and the contract of this function. You may use the
3331
// comment of the test below as your format reference.
3432
unsafe {
35-
todo!("Your code goes here")
33+
*(address as *mut u32) = 0xAABBCCDD;
3634
}
3735
}
3836

exercises/tests/tests6.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
// Execute `rustlings hint tests6` or use the `hint` watch subcommand for a
88
// hint.
99

10-
// I AM NOT DONE
1110

1211
struct Foo {
1312
a: u128,
@@ -20,8 +19,9 @@ struct Foo {
2019
unsafe fn raw_pointer_to_box(ptr: *mut Foo) -> Box<Foo> {
2120
// SAFETY: The `ptr` contains an owned box of `Foo` by contract. We
2221
// simply reconstruct the box from that pointer.
23-
let mut ret: Box<Foo> = unsafe { ??? };
24-
todo!("The rest of the code goes here")
22+
let mut ret: Box<Foo> = unsafe { Box::from_raw(ptr) };
23+
ret.b = Some("hello".to_owned());
24+
ret
2525
}
2626

2727
#[cfg(test)]

exercises/tests/tests7.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@
3434
// Execute `rustlings hint tests7` or use the `hint` watch subcommand for a
3535
// hint.
3636

37-
// I AM NOT DONE
38-
3937
fn main() {}
4038

4139
#[cfg(test)]

exercises/tests/tests8.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
// Execute `rustlings hint tests8` or use the `hint` watch subcommand for a
88
// hint.
99

10-
// I AM NOT DONE
11-
1210
fn main() {}
1311

1412
#[cfg(test)]

exercises/traits/traits3.rs

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,42 @@
1-
impl AppendBar for Vec<String> {
2-
fn append_bar(mut self) -> Self {
3-
self.push(String::from("Bar"));
4-
self
1+
// traits3.rs
2+
//
3+
// Your task is to implement the Licensed trait for both structures and have
4+
// them return the same information without writing the same function twice.
5+
//
6+
// Consider what you can add to the Licensed trait.
7+
//
8+
// Execute `rustlings hint traits3` or use the `hint` watch subcommand for a
9+
// hint.
10+
11+
pub trait Licensed {
12+
fn licensing_info(&self) -> String {
13+
String::from("Some information")
14+
}
15+
}
16+
17+
struct SomeSoftware {
18+
version_number: i32,
19+
}
20+
21+
struct OtherSoftware {
22+
version_number: String,
23+
}
24+
25+
impl Licensed for SomeSoftware {} // Don't edit this line
26+
impl Licensed for OtherSoftware {} // Don't edit this line
27+
28+
#[cfg(test)]
29+
mod tests {
30+
use super::*;
31+
32+
#[test]
33+
fn is_licensing_info_the_same() {
34+
let licensing_info = String::from("Some information");
35+
let some_software = SomeSoftware { version_number: 1 };
36+
let other_software = OtherSoftware {
37+
version_number: "v2.0.0".to_string(),
38+
};
39+
assert_eq!(some_software.licensing_info(), licensing_info);
40+
assert_eq!(other_software.licensing_info(), licensing_info);
541
}
642
}

0 commit comments

Comments
 (0)