Skip to content

Commit 0ba0677

Browse files
committed
do exec
1 parent da40ac6 commit 0ba0677

File tree

7 files changed

+26
-21
lines changed

7 files changed

+26
-21
lines changed

exercises/conversions/as_ref_mut.rs

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

10-
// I AM NOT DONE
1110

1211
// Obtain the number of bytes (not characters) in the given argument.
1312
// TODO: Add the AsRef trait appropriately as a trait bound.
14-
fn byte_counter<T>(arg: T) -> usize {
13+
fn byte_counter<T: AsRef<str>>(arg: T) -> usize {
1514
arg.as_ref().as_bytes().len()
1615
}
1716

1817
// Obtain the number of characters (not bytes) in the given argument.
1918
// TODO: Add the AsRef trait appropriately as a trait bound.
20-
fn char_counter<T>(arg: T) -> usize {
19+
fn char_counter<T: AsRef<str>>(arg: T) -> usize {
2120
arg.as_ref().chars().count()
2221
}
2322

2423
// Squares a number using as_mut().
2524
// TODO: Add the appropriate trait bound.
26-
fn num_sq<T>(arg: &mut T) {
25+
fn num_sq<T, U>(arg: &mut T)
26+
where
27+
T: AsMut<U>,
28+
U: std::ops::Mul<Output = U> + Copy,
29+
{
2730
// TODO: Implement the function body.
28-
???
31+
let inner= arg.as_mut();
32+
*inner = (*inner) * (*inner);
2933
}
3034

3135
#[cfg(test)]

exercises/tests/build.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,13 @@ 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-
);
17-
println!("cargo:{}", your_command);
13+
// Tell Cargo to set TEST_FOO for the compiled crate so the
14+
// test can read it at runtime.
15+
println!("cargo:rustc-env=TEST_FOO={}", timestamp);
1816

1917
// In tests8, we should enable "pass" feature to make the
2018
// testcase return early. Fill in the command to tell
2119
// Cargo about that.
22-
let your_command = "Your command here, please checkout exercises/tests/build.rs";
20+
let your_command = "rustc-cfg=feature=\"pass\"";
2321
println!("cargo:{}", your_command);
2422
}

exercises/tests/tests5.rs

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

25-
// I AM NOT DONE
2625

2726
/// # Safety
2827
///
@@ -32,7 +31,9 @@ unsafe fn modify_by_address(address: usize) {
3231
// code's behavior and the contract of this function. You may use the
3332
// comment of the test below as your format reference.
3433
unsafe {
35-
todo!("Your code goes here")
34+
// todo!("Your code goes here")
35+
let v = address as *mut u32;
36+
*v = 0xAABBCCDD;
3637
}
3738
}
3839

exercises/tests/tests6.rs

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

10-
// I AM NOT DONE
11-
1210
struct Foo {
1311
a: u128,
1412
b: Option<String>,
@@ -20,8 +18,14 @@ struct Foo {
2018
unsafe fn raw_pointer_to_box(ptr: *mut Foo) -> Box<Foo> {
2119
// SAFETY: The `ptr` contains an owned box of `Foo` by contract. We
2220
// simply reconstruct the box from that pointer.
23-
let mut ret: Box<Foo> = unsafe { ??? };
24-
todo!("The rest of the code goes here")
21+
let mut ret: Box<Foo> = unsafe {
22+
(*ptr).b =Some("hello".to_string());
23+
24+
Box::from_raw(ptr)
25+
};
26+
27+
ret
28+
// todo!("The rest of the code goes here")
2529
}
2630

2731
#[cfg(test)]

exercises/tests/tests7.rs

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

37-
// I AM NOT DONE
3837

3938
fn main() {}
4039

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/tests/tests9.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,16 @@
2727
//
2828
// You should NOT modify any existing code except for adding two lines of attributes.
2929

30-
// I AM NOT DONE
3130

3231
extern "Rust" {
3332
fn my_demo_function(a: u32) -> u32;
33+
#[link_name = "my_demo_function"]
3434
fn my_demo_function_alias(a: u32) -> u32;
3535
}
3636

3737
mod Foo {
3838
// No `extern` equals `extern "Rust"`.
39+
#[no_mangle]
3940
fn my_demo_function(a: u32) -> u32 {
4041
a
4142
}

0 commit comments

Comments
 (0)