Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
saiyuan233 committed Dec 26, 2024
1 parent 01969ad commit 841da89
Show file tree
Hide file tree
Showing 13 changed files with 47 additions and 32 deletions.
10 changes: 5 additions & 5 deletions exercises/iterators/iterators1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@
// Execute `rustlings hint iterators1` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE


fn main() {
let my_fav_fruits = vec!["banana", "custard apple", "avocado", "peach", "raspberry"];

let mut my_iterable_fav_fruits = ???; // TODO: Step 1
let mut my_iterable_fav_fruits = my_fav_fruits.iter(); // TODO: Step 1

assert_eq!(my_iterable_fav_fruits.next(), Some(&"banana"));
assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 2
assert_eq!(my_iterable_fav_fruits.next(), Some(&"custard apple")); // TODO: Step 2
assert_eq!(my_iterable_fav_fruits.next(), Some(&"avocado"));
assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 3
assert_eq!(my_iterable_fav_fruits.next(), Some(&"peach")); // TODO: Step 3
assert_eq!(my_iterable_fav_fruits.next(), Some(&"raspberry"));
assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 4
assert_eq!(my_iterable_fav_fruits.next(), None); // TODO: Step 4
}
2 changes: 1 addition & 1 deletion exercises/iterators/iterators2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
pub fn capitalize_first(input: &str) -> String {
let mut c = input.chars();
match c.next() {
None => String::new(),
None => String::new(),
Some(first) => ???,
}
}
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"
9 changes: 2 additions & 7 deletions exercises/tests/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,10 @@ 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);
println!("cargo:rustc-env=TEST_FOO={}", timestamp);

// 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);
println!("cargo:rustc-cfg=pass");
}
4 changes: 2 additions & 2 deletions exercises/tests/tests1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
// Execute `rustlings hint tests1` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE


#[cfg(test)]
mod tests {
#[test]
fn you_can_assert() {
assert!();
assert!(true);
}
}
4 changes: 2 additions & 2 deletions exercises/tests/tests2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
// Execute `rustlings hint tests2` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE


#[cfg(test)]
mod tests {
#[test]
fn you_can_assert_eq() {
assert_eq!();
assert_eq!(true, true);
}
}
6 changes: 3 additions & 3 deletions exercises/tests/tests3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// Execute `rustlings hint tests3` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE


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

#[test]
fn is_true_when_even() {
assert!();
assert!(is_even(4), "Expected 4 to be even");
}

#[test]
fn is_false_when_odd() {
assert!();
assert!(!is_even(5),"Expected 5 to be odd");
}
}
8 changes: 5 additions & 3 deletions exercises/tests/tests4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Execute `rustlings hint tests4` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE


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

#[test]
#[should_panic]
fn negative_width() {
// This test should check if program panics when we try to create rectangle with negative width
let _rect = Rectangle::new(-10, 10);
}

#[test]
#[should_panic]
fn negative_height() {
// This test should check if program panics when we try to create rectangle with negative height
let _rect = Rectangle::new(10, -10);
Expand Down
7 changes: 5 additions & 2 deletions exercises/tests/tests5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
// Execute `rustlings hint tests5` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE


/// # Safety
///
Expand All @@ -32,7 +32,10 @@ unsafe fn modify_by_address(address: usize) {
// 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")
// 将 `usize` 地址转换为 `*mut u32` 类型的原始指针
let ptr = address as *mut u32;
// 解引用指针并修改其值
*ptr = 0xAABBCCDD;
}
}

Expand Down
8 changes: 4 additions & 4 deletions exercises/tests/tests6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// Execute `rustlings hint tests6` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE


struct Foo {
a: u128,
Expand All @@ -20,8 +20,8 @@ struct Foo {
unsafe fn raw_pointer_to_box(ptr: *mut Foo) -> Box<Foo> {
// SAFETY: The `ptr` contains an owned box of `Foo` by contract. We
// simply reconstruct the box from that pointer.
let mut ret: Box<Foo> = unsafe { ??? };
todo!("The rest of the code goes here")
let mut ret: Box<Foo> = unsafe { Box::from_raw(ptr) };
ret
}

#[cfg(test)]
Expand All @@ -31,7 +31,7 @@ mod tests {

#[test]
fn test_success() {
let data = Box::new(Foo { a: 1, b: None });
let data = Box::new(Foo { a: 1, b: Some("hello".to_owned()) });

let ptr_1 = &data.a as *const u128 as usize;
// SAFETY: We pass an owned box of `Foo`.
Expand Down
2 changes: 1 addition & 1 deletion exercises/tests/tests7.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
// Execute `rustlings hint tests7` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE


fn main() {}

Expand Down
5 changes: 3 additions & 2 deletions exercises/tests/tests8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// Execute `rustlings hint tests8` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE


fn main() {}

Expand All @@ -17,9 +17,10 @@ mod tests {

#[test]
fn test_success() {
#[cfg(feature = "pass")]
#[cfg(pass)]
return;

#[cfg(not(pass))]
panic!("no cfg set");
}
}

0 comments on commit 841da89

Please sign in to comment.