Skip to content

Commit 6a78a98

Browse files
committed
update
1 parent fe82888 commit 6a78a98

13 files changed

Lines changed: 83 additions & 30 deletions

File tree

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: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
//! This is the build script for both tests7 and tests8.
22
//!
33
//! You should modify this file to make both exercises pass.
4-
4+
use std::time::SystemTime;
5+
use std::env;
56
fn main() {
67
// In tests7, we should set up an environment variable
78
// called `TEST_FOO`. Print in the standard output to let
@@ -10,15 +11,25 @@ fn main() {
1011
.duration_since(std::time::UNIX_EPOCH)
1112
.unwrap()
1213
.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);
14+
// let your_command = format!(
15+
// //"Your command here with {}, please checkout exercises/tests/build.rs",
16+
//
17+
// timestamp
18+
// );
19+
// println!("cargo:{}", your_command);
20+
//
21+
// // In tests8, we should enable "pass" feature to make the
22+
// // testcase return early. Fill in the command to tell
23+
// // Cargo about that.
24+
// let your_command = "Your command here, please checkout exercises/tests/build.rs";
25+
// println!("cargo:{}", your_command);
26+
// 修正:直接使用 Cargo 专属格式设置 TEST_FOO 环境变量(移除错误的 format! 嵌套 println!)
27+
println!("cargo:rustc-env=TEST_FOO={}", timestamp);
1828

1929
// In tests8, we should enable "pass" feature to make the
2030
// testcase return early. Fill in the command to tell
2131
// Cargo about that.
22-
let your_command = "Your command here, please checkout exercises/tests/build.rs";
32+
// 启用 "pass" 特性的 Cargo 指令
33+
let your_command = "rustc-cfg=feature=\"pass\"";
2334
println!("cargo:{}", your_command);
2435
}

exercises/tests/tests3.rs

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

10-
// I AM NOT DONE
10+
// I AM NOT
1111

1212
pub fn is_even(num: i32) -> bool {
1313
num % 2 == 0
@@ -19,11 +19,11 @@ mod tests {
1919

2020
#[test]
2121
fn is_true_when_even() {
22-
assert!();
22+
assert!(is_even(2));
2323
}
2424

2525
#[test]
2626
fn is_false_when_odd() {
27-
assert!();
27+
assert!(!is_even(5));
2828
}
2929
}

exercises/tests/tests4.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// Execute `rustlings hint tests4` or use the `hint` watch subcommand for a
66
// hint.
77

8-
// I AM NOT DONE
8+
// I AM NOT
99

1010
struct Rectangle {
1111
width: i32,
@@ -30,17 +30,19 @@ mod tests {
3030
fn correct_width_and_height() {
3131
// This test should check if the rectangle is the size that we pass into its constructor
3232
let rect = Rectangle::new(10, 20);
33-
assert_eq!(???, 10); // check width
34-
assert_eq!(???, 20); // check height
33+
assert_eq!(rect.width, 10); // check width
34+
assert_eq!(rect.height, 20); // check height
3535
}
3636

3737
#[test]
38+
#[should_panic(expected = "Rectangle width and height cannot be negative!")]
3839
fn negative_width() {
3940
// This test should check if program panics when we try to create rectangle with negative width
4041
let _rect = Rectangle::new(-10, 10);
4142
}
4243

4344
#[test]
45+
#[should_panic(expected = "Rectangle width and height cannot be negative!")]
4446
fn negative_height() {
4547
// This test should check if program panics when we try to create rectangle with negative height
4648
let _rect = Rectangle::new(10, -10);

exercises/tests/tests5.rs

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

25-
// I AM NOT DONE
25+
// I AM NOT
2626

2727
/// # Safety
2828
///
@@ -32,7 +32,10 @@ unsafe fn modify_by_address(address: usize) {
3232
// code's behavior and the contract of this function. You may use the
3333
// comment of the test below as your format reference.
3434
unsafe {
35-
todo!("Your code goes here")
35+
//todo!("Your code goes here")
36+
let ptr = address as *mut u32;
37+
// 2. 解引用原始指针,并赋值为目标值 0xAABBCCDD
38+
*ptr = 0xAABBCCDD;
3639
}
3740
}
3841

exercises/tests/tests6.rs

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

10-
// I AM NOT DONE
10+
// I AM NOT
1111

1212
struct Foo {
1313
a: u128,
@@ -20,8 +20,13 @@ struct Foo {
2020
unsafe fn raw_pointer_to_box(ptr: *mut Foo) -> Box<Foo> {
2121
// SAFETY: The `ptr` contains an owned box of `Foo` by contract. We
2222
// simply reconstruct the box from that pointer.
23-
let mut ret: Box<Foo> = unsafe { ??? };
24-
todo!("The rest of the code goes here")
23+
let mut ret: Box<Foo> = unsafe {
24+
Box::from_raw(ptr)
25+
};
26+
//todo!("The rest of the code goes here")
27+
ret.b = Some("hello".to_owned());
28+
// 返回修改后的 Box<Foo>
29+
ret
2530
}
2631

2732
#[cfg(test)]

exercises/tests/tests7.rs

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

37-
// I AM NOT DONE
37+
// I AM NOT
3838

3939
fn main() {}
4040

@@ -51,5 +51,7 @@ mod tests {
5151
let s = std::env::var("TEST_FOO").unwrap();
5252
let e: u64 = s.parse().unwrap();
5353
assert!(timestamp >= e && timestamp < e + 10);
54+
55+
5456
}
5557
}

exercises/tests/tests8.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// Execute `rustlings hint tests8` or use the `hint` watch subcommand for a
88
// hint.
99

10-
// I AM NOT DONE
10+
// I AM NOT
1111

1212
fn main() {}
1313

exercises/tests/tests9.rs

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

30-
// I AM NOT DONE
30+
// I AM NOT
3131

3232
extern "Rust" {
3333
fn my_demo_function(a: u32) -> u32;
34+
#[link_name = "my_demo_function"]
3435
fn my_demo_function_alias(a: u32) -> u32;
3536
}
3637

3738
mod Foo {
3839
// No `extern` equals `extern "Rust"`.
40+
41+
42+
43+
#[no_mangle]
44+
45+
46+
// No `extern` equals `extern "Rust"`.
3947
fn my_demo_function(a: u32) -> u32 {
4048
a
4149
}

0 commit comments

Comments
 (0)