File tree Expand file tree Collapse file tree 7 files changed +26
-21
lines changed Expand file tree Collapse file tree 7 files changed +26
-21
lines changed Original file line number Diff line number Diff line change 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) ]
Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 77// Execute `rustlings hint tests6` or use the `hint` watch subcommand for a
88// hint.
99
10- // I AM NOT DONE
11-
1210struct Foo {
1311 a : u128 ,
1412 b : Option < String > ,
@@ -20,8 +18,14 @@ struct Foo {
2018unsafe 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) ]
Original file line number Diff line number Diff line change 3434// Execute `rustlings hint tests7` or use the `hint` watch subcommand for a
3535// hint.
3636
37- // I AM NOT DONE
3837
3938fn main ( ) { }
4039
Original file line number Diff line number Diff line change 77// Execute `rustlings hint tests8` or use the `hint` watch subcommand for a
88// hint.
99
10- // I AM NOT DONE
11-
1210fn main ( ) { }
1311
1412#[ cfg( test) ]
Original file line number Diff line number Diff line change 2727//
2828// You should NOT modify any existing code except for adding two lines of attributes.
2929
30- // I AM NOT DONE
3130
3231extern "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
3737mod Foo {
3838 // No `extern` equals `extern "Rust"`.
39+ #[ no_mangle]
3940 fn my_demo_function ( a : u32 ) -> u32 {
4041 a
4142 }
You can’t perform that action at this time.
0 commit comments