mirror of
https://github.com/rust-lang/rustlings
synced 2024-11-10 14:44:25 +00:00
Update the readme links, oops
This commit is contained in:
parent
0cc668c012
commit
6a376c1e3d
1 changed files with 5 additions and 5 deletions
10
README.md
10
README.md
|
@ -50,7 +50,7 @@ following exercises will ask you to make tests pass!
|
|||
|
||||
[Testing chapter from the Effective Rust section of the book](https://doc.rust-lang.org/stable/book/testing.html)
|
||||
|
||||
- ["tests1.rs"](http://play.rust-lang.org/?code=%2F%2F+This+test+has+a+problem+with+it+--+make+the+test+compile%21+Make+the+test%0A%2F%2F+pass%21+Make+the+test+fail%21+Scroll+down+for+hints+%3A%29%0A%0A%23%5Bcfg%28test%29%5D%0Amod+tests+%7B%0A++++%23%5Btest%5D%0A++++fn+you_can_assert%28%29+%7B%0A++++++++assert%21%28%29%3B%0A++++%7D%0A%7D%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%2F%2F+You+don%27t+even+need+to+write+any+code+to+test+--+you+can+just+test+values+and+run+that%2C+even%0A%2F%2F+though+you+wouldn%27t+do+that+in+real+life+%3A%29+%60assert%21%60+is+a+macro+that+needs+an+argument.%0A%2F%2F+Depending+on+the+value+of+the+argument%2C+%60assert%21%60+will+do+nothing+%28in+which+case+the+test+will%0A%2F%2F+pass%29+or+%60assert%21%60+will+panic+%28in+which+case+the+test+will+fail%29.+So+try+giving+different+values%0A%2F%2F+to+%60assert%21%60+and+see+which+ones+compile%2C+which+ones+pass%2C+and+which+ones+fail+%3A%29%0A)
|
||||
- ["tests1.rs"](http://play.rust-lang.org/?code=%2F%2F+Tests+are+important+to+ensure+that+your+code+does+what+you+think+it+should+do.%0A%2F%2F+Tests+can+be+run+on+this+file+with+the+following+command%3A%0A%2F%2F+rustc+--test+tests1.rs%0A%0A%2F%2F+This+test+has+a+problem+with+it+--+make+the+test+compile%21+Make+the+test%0A%2F%2F+pass%21+Make+the+test+fail%21+Scroll+down+for+hints+%3A%29%0A%0A%23%5Bcfg%28test%29%5D%0Amod+tests+%7B%0A++++%23%5Btest%5D%0A++++fn+you_can_assert%28%29+%7B%0A++++++++assert%21%28%29%3B%0A++++%7D%0A%7D%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%2F%2F+You+don%27t+even+need+to+write+any+code+to+test+--+you+can+just+test+values+and+run+that%2C+even%0A%2F%2F+though+you+wouldn%27t+do+that+in+real+life+%3A%29+%60assert%21%60+is+a+macro+that+needs+an+argument.%0A%2F%2F+Depending+on+the+value+of+the+argument%2C+%60assert%21%60+will+do+nothing+%28in+which+case+the+test+will%0A%2F%2F+pass%29+or+%60assert%21%60+will+panic+%28in+which+case+the+test+will+fail%29.+So+try+giving+different+values%0A%2F%2F+to+%60assert%21%60+and+see+which+ones+compile%2C+which+ones+pass%2C+and+which+ones+fail+%3A%29%0A)
|
||||
- ["tests2.rs"](http://play.rust-lang.org/?code=%2F%2F+This+test+has+a+problem+with+it+--+make+the+test+compile%21+Make+the+test%0A%2F%2F+pass%21+Make+the+test+fail%21+Scroll+down+for+hints+%3A%29%0A%0A%23%5Bcfg%28test%29%5D%0Amod+tests+%7B%0A++++%23%5Btest%5D%0A++++fn+you_can_assert_eq%28%29+%7B%0A++++++++assert_eq%21%28%29%3B%0A++++%7D%0A%7D%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%2F%2F+Like+the+previous+exercise%2C+you+don%27t+need+to+write+any+code+to+get+this+test+to+compile+and%0A%2F%2F+run.+%60assert_eq%21%60+is+a+macro+that+takes+two+arguments+and+compares+them.+Try+giving+it+two%0A%2F%2F+values+that+are+equal%21+Try+giving+it+two+arguments+that+are+different%21+Try+giving+it+two+values%0A%2F%2F+that+are+of+different+types%21+Try+switching+which+argument+comes+first+and+which+comes+second%21%0A)
|
||||
- ["tests3.rs"](http://play.rust-lang.org/?code=%2F%2F+This+test+isn%27t+testing+our+function+--+make+it+do+that+in+such+a+way+that%0A%2F%2F+the+test+passes.+Then+write+a+second+test+that+tests+that+we+get+the+result%0A%2F%2F+we+expect+to+get+when+we+call+%60is_even%285%29%60.+Scroll+down+for+hints%21%0A%0Apub+fn+is_even%28num%3A+i32%29+-%3E+bool+%7B%0A++++num+%25+2+%3D%3D+0%0A%7D%0A%0A%23%5Bcfg%28test%29%5D%0Amod+tests+%7B%0A++++use+super%3A%3A*%3B%0A%0A++++%23%5Btest%5D%0A++++fn+is_true_when_even%28%29+%7B%0A++++++++assert%21%28false%29%3B%0A++++%7D%0A%7D%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%2F%2F+You+can+call+a+function+right+where+you%27re+passing+arguments+to+%60assert%21%60+--+so+you+could+do%0A%2F%2F+something+like+%60assert%21%28having_fun%28%29%29%60.+If+you+want+to+check+that+you+indeed+get+false%2C+you%0A%2F%2F+can+negate+the+result+of+what+you%27re+doing+using+%60%21%60%2C+like+%60assert%21%28%21having_fun%28%29%29%60.%0A)
|
||||
- ["tests4.rs"](http://play.rust-lang.org/?code=%2F%2F+This+test+isn%27t+testing+our+function+--+make+it+do+that+in+such+a+way+that%0A%2F%2F+the+test+passes.+Then+write+a+second+test+that+tests+that+we+get+the+result%0A%2F%2F+we+expect+to+get+when+we+call+%60times_two%60+with+a+negative+number.%0A%2F%2F+No+hints%2C+you+can+do+this+%3A%29%0A%0Apub+fn+times_two%28num%3A+i32%29+-%3E+i32+%7B%0A++++num+*+2%0A%7D%0A%0A%23%5Bcfg%28test%29%5D%0Amod+tests+%7B%0A++++use+super%3A%3A*%3B%0A%0A++++%23%5Btest%5D%0A++++fn+returns_twice_of_positive_numbers%28%29+%7B%0A++++++++assert_eq%21%284%2C+4%29%3B%0A++++%7D%0A%7D%0A)
|
||||
|
@ -59,15 +59,15 @@ following exercises will ask you to make tests pass!
|
|||
|
||||
[Relevant chapter in The Rust Programming Language](https://doc.rust-lang.org/stable/book/if.html)
|
||||
|
||||
- ["if1.rs"](http://play.rust-lang.org/?code=fn+bigger%28a%3A+i32%2C+b%3Ai32%29+-%3E+i32+%7B%0A++++%2F%2F+Complete+this+function+to+return+the+bigger+number%21%0A++++%2F%2F+Do+not+use%3A%0A++++%2F%2F+-+return%0A++++%2F%2F+-+another+function+call%0A++++%2F%2F+-+additional+variables%0A++++%2F%2F+Scroll+down+for+hints.%0A%7D%0A%0Afn+main%28%29+%7B%0A++++assert_eq%21%2810%2C+bigger%2810%2C+8%29%29%3B%0A++++assert_eq%21%2842%2C+bigger%2832%2C+42%29%29%3B%0A%7D%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%2F%2F+It%27s+possible+to+do+this+in+one+line+if+you+would+like%21%0A%2F%2F+Some+similar+examples+from+other+languages%3A%0A%2F%2F+-+In+C%28%2B%2B%29+this+would+be%3A+%60a+%3E+b+%3F+a+%3A+b%60%0A%2F%2F+-+In+Python+this+would+be%3A++%60a+if+a+%3E+b+else+b%60%0A%2F%2F+Remember+in+Rust+that%3A%0A%2F%2F+-+the+%60if%60+condition+does+not+need+to+be+surrounded+by+parentheses%0A%2F%2F+-+%60if%60%2F%60else%60+conditionals+are+expressions%0A%2F%2F+-+Each+condition+is+followed+by+a+%60%7B%7D%60+block.%0A)
|
||||
- ["if1.rs"](http://play.rust-lang.org/?code=pub+fn+bigger%28a%3A+i32%2C+b%3Ai32%29+-%3E+i32+%7B%0A++++%2F%2F+Complete+this+function+to+return+the+bigger+number%21%0A++++%2F%2F+Do+not+use%3A%0A++++%2F%2F+-+return%0A++++%2F%2F+-+another+function+call%0A++++%2F%2F+-+additional+variables%0A++++%2F%2F+Scroll+down+for+hints.%0A%7D%0A%0A%23%5Bcfg%28test%29%5D%0Amod+tests+%7B%0A++++use+super%3A%3A*%3B%0A%0A++++%23%5Btest%5D%0A++++fn+ten_is_bigger_than_eight%28%29+%7B%0A++++++++assert_eq%21%2810%2C+bigger%2810%2C+8%29%29%3B%0A++++%7D%0A%0A++++%23%5Btest%5D%0A++++fn+fortytwo_is_bigger_than_thirtytwo%28%29+%7B%0A++++++++assert_eq%21%2842%2C+bigger%2832%2C+42%29%29%3B%0A++++%7D%0A%7D%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%2F%2F+It%27s+possible+to+do+this+in+one+line+if+you+would+like%21%0A%2F%2F+Some+similar+examples+from+other+languages%3A%0A%2F%2F+-+In+C%28%2B%2B%29+this+would+be%3A+%60a+%3E+b+%3F+a+%3A+b%60%0A%2F%2F+-+In+Python+this+would+be%3A++%60a+if+a+%3E+b+else+b%60%0A%2F%2F+Remember+in+Rust+that%3A%0A%2F%2F+-+the+%60if%60+condition+does+not+need+to+be+surrounded+by+parentheses%0A%2F%2F+-+%60if%60%2F%60else%60+conditionals+are+expressions%0A%2F%2F+-+Each+condition+is+followed+by+a+%60%7B%7D%60+block.%0A)
|
||||
|
||||
### Strings
|
||||
|
||||
[Relevant chapter in The Rust Programming Language](https://doc.rust-lang.org/stable/book/strings.html)
|
||||
|
||||
- ["strings1.rs"](http://play.rust-lang.org/?code=%2F%2F+Make+me+compile+without+changing+the+function+signature%21+Scroll+down+for+hints+%3A%29%0A%0Afn+main%28%29+%7B%0A++++let+answer+%3D+current_favorite_color%28%29%3B%0A++++println%21%28%22My+current+favorite+color+is+%7B%7D%22%2C+answer%29%3B%0A%7D%0A%0Afn+current_favorite_color%28%29+-%3E+String+%7B%0A++++%22blue%22%0A%7D%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%2F%2F+The+%60current_favorite_color%60+function+is+currently+returning+a+string+slice+with+the+%60%27static%60%0A%2F%2F+lifetime.+We+know+this+because+the+data+of+the+string+lives+in+our+code+itself+--+it+doesn%27t%0A%2F%2F+come+from+a+file+or+user+input+or+another+program+--+so+it+will+live+as+long+as+our+program%0A%2F%2F+lives.+But+it+is+still+a+string+slice.+There%27s+one+way+to+create+a+%60String%60+by+converting+a%0A%2F%2F+string+slice+covered+in+the+Strings+chapter+of+the+book%2C+and+another+way+that+uses+the+%60From%60%0A%2F%2F+trait.%0A)
|
||||
- ["strings2.rs"](http://play.rust-lang.org/?code=%2F%2F+Make+me+compile+without+changing+the+function+signature%21+Scroll+down+for+hints+%3A%29%0A%0Afn+main%28%29+%7B%0A++++let+word+%3D+String%3A%3Afrom%28%22green%22%29%3B+%2F%2F+Try+not+changing+this+line+%3A%29%0A++++if+is_a_color_word%28word%29+%7B%0A++++++++println%21%28%22That+is+a+color+word+I+know%21%22%29%3B%0A++++%7D+else+%7B%0A++++++++println%21%28%22That+is+not+a+color+word+I+know.%22%29%3B%0A++++%7D%0A%7D%0A%0Afn+is_a_color_word%28attempt%3A+%26str%29+-%3E+bool+%7B%0A++++attempt+%3D%3D+%22green%22+%7C%7C+attempt+%3D%3D+%22blue%22+%7C%7C+attempt+%3D%3D+%22red%22%0A%7D%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%2F%2F+Yes%2C+it+would+be+really+easy+to+fix+this+by+just+changing+the+value+bound+to+%60guess1%60+to+be+a%0A%2F%2F+string+slice+instead+of+a+%60String%60%2C+wouldn%27t+it%3F%3F+There+is+a+way+to+add+one+character+to+line%0A%2F%2F+5%2C+though%2C+that+will+coerce+the+%60String%60+into+a+string+slice.%0A)
|
||||
- ["strings3.rs"](http://play.rust-lang.org/?code=%2F%2F+Ok%2C+here+are+a+bunch+of+values--+some+are+%60Strings%60%2C+some+are+%60%26strs%60.+Your%0A%2F%2F+task+is+to+call+one+of+these+two+functions+on+each+value+depending+on+what%0A%2F%2F+you+think+each+value+is.+That+is%2C+add+either+%60string_slice%60+or+%60string%60%0A%2F%2F+before+the+parentheses+on+each+line.+If+you%27re+right%2C+it+will+compile%21%0A%0Afn+string_slice%28arg%3A+%26str%29+%7B+println%21%28%22%7B%7D%22%2C+arg%29%3B+%7D%0Afn+string%28arg%3A+String%29+%7B+println%21%28%22%7B%7D%22%2C+arg%29%3B+%7D%0A%0Afn+main%28%29+%7B%0A++++%28%22blue%22%29%3B%0A++++%28%22red%22.to_string%28%29%29%3B%0A++++%28String%3A%3Afrom%28%22hi%22%29%29%3B%0A++++%28format%21%28%22Interpolation+%7B%7D%22%2C+%22Station%22%29%29%3B%0A++++%28%26String%3A%3Afrom%28%22abc%22%29%5B0..1%5D%29%3B%0A++++%28%22++hello+there+%22.trim%28%29%29%3B%0A++++%28%22Happy+Monday%21%22.to_string%28%29.replace%28%22Mon%22%2C+%22Tues%22%29%29%3B%0A++++%28%22mY+sHiFt+KeY+iS+sTiCkY%22.to_lowercase%28%29%29%3B%0A%7D%0A)
|
||||
- ["strings2.rs"](http://play.rust-lang.org/?code=%2F%2F+Make+me+compile+without+changing+the+function+signature%21+Scroll+down+for+hints+%3A%29%0A%0Afn+main%28%29+%7B%0A++++let+word+%3D+String%3A%3Afrom%28%22green%22%29%3B+%2F%2F+Try+not+changing+this+line+%3A%29%0A++++if+is_a_color_word%28word%29+%7B%0A++++++++println%21%28%22That+is+a+color+word+I+know%21%22%29%3B%0A++++%7D+else+%7B%0A++++++++println%21%28%22That+is+not+a+color+word+I+know.%22%29%3B%0A++++%7D%0A%7D%0A%0Afn+is_a_color_word%28attempt%3A+%26str%29+-%3E+bool+%7B%0A++++attempt+%3D%3D+%22green%22+%7C%7C+attempt+%3D%3D+%22blue%22+%7C%7C+attempt+%3D%3D+%22red%22%0A%7D%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%2F%2F+Yes%2C+it+would+be+really+easy+to+fix+this+by+just+changing+the+value+bound+to+%60word%60+to+be+a%0A%2F%2F+string+slice+instead+of+a+%60String%60%2C+wouldn%27t+it%3F%3F+There+is+a+way+to+add+one+character+to+line%0A%2F%2F+5%2C+though%2C+that+will+coerce+the+%60String%60+into+a+string+slice.%0A)
|
||||
- ["strings3.rs"](http://play.rust-lang.org/?code=%2F%2F+Ok%2C+here+are+a+bunch+of+values--+some+are+%60Strings%60%2C+some+are+%60%26strs%60.+Your%0A%2F%2F+task+is+to+call+one+of+these+two+functions+on+each+value+depending+on+what%0A%2F%2F+you+think+each+value+is.+That+is%2C+add+either+%60string_slice%60+or+%60string%60%0A%2F%2F+before+the+parentheses+on+each+line.+If+you%27re+right%2C+it+will+compile%21%0A%0Afn+string_slice%28arg%3A+%26str%29+%7B+println%21%28%22%7B%7D%22%2C+arg%29%3B+%7D%0Afn+string%28arg%3A+String%29+%7B+println%21%28%22%7B%7D%22%2C+arg%29%3B+%7D%0A%0Afn+main%28%29+%7B%0A++++%28%22blue%22%29%3B%0A++++%28%22red%22.to_string%28%29%29%3B%0A++++%28String%3A%3Afrom%28%22hi%22%29%29%3B%0A++++%28%22rust+is+fun%21%22.to_owned%28%29%29%3B%0A++++%28%22nice+weather%22.into%28%29%29%3B%0A++++%28format%21%28%22Interpolation+%7B%7D%22%2C+%22Station%22%29%29%3B%0A++++%28%26String%3A%3Afrom%28%22abc%22%29%5B0..1%5D%29%3B%0A++++%28%22++hello+there+%22.trim%28%29%29%3B%0A++++%28%22Happy+Monday%21%22.to_string%28%29.replace%28%22Mon%22%2C+%22Tues%22%29%29%3B%0A++++%28%22mY+sHiFt+KeY+iS+sTiCkY%22.to_lowercase%28%29%29%3B%0A%7D%0A)
|
||||
|
||||
### Move semantics
|
||||
|
||||
|
@ -120,7 +120,7 @@ Check out the [Iterators chapter of the book](https://doc.rust-lang.org/stable/b
|
|||
|
||||
See [the Dining Philosophers example](https://doc.rust-lang.org/1.4.0/book/dining-philosophers.html) and the [Concurrency Chapter](https://doc.rust-lang.org/stable/book/concurrency.html) from the book.
|
||||
|
||||
- ["threads1.rs"](<https://play.rust-lang.org/?code=%2F%2F%20Make%20this%20compile!%20Scroll%20down%20for%20hints%20%3A\)%20The%20idea%20is%20the%20thread%0A%2F%2F%20spawned%20on%20line%2017%20is%20completing%20jobs%20while%20the%20main%20thread%20is%0A%2F%2F%20monitoring%20progress%20until%2010%20jobs%20are%20completed.%20If%20you%20see%206%20lines%0A%2F%2F%20of%20%22waiting...%22%20and%20the%20program%20ends%20without%20timing%20out%20the%20playground%2C%0A%2F%2F%20you%27ve%20got%20it%20%3A\)%0A%0Ause%20std%3A%3Async%3A%3AArc%3B%0Ause%20std%3A%3Athread%3B%0Ause%20std%3A%3Atime%3A%3ADuration%3B%0A%0Astruct%20JobStatus%20%7B%0A%20%20%20%20jobs_completed%3A%20u32%2C%0A%7D%0A%0Afn%20main\(\)%20%7B%0A%20%20%20%20let%20status%20%3D%20Arc%3A%3Anew\(JobStatus%20%7B%20jobs_completed%3A%200%20%7D\)%3B%0A%20%20%20%20let%20status_shared%20%3D%20status.clone\(\)%3B%0A%20%20%20%20thread%3A%3Aspawn\(move%20%7C%7C%20%7B%0A%20%20%20%20%20%20%20%20for%20_%20in%200..10%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20thread%3A%3Asleep\(Duration%3A%3Afrom_millis\(250\)\)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20status_shared.jobs_completed%20%2B%3D%201%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D\)%3B%0A%20%20%20%20while%20status.jobs_completed%20%3C%2010%20%7B%0A%20%20%20%20%20%20%20%20println!\(%22waiting...%20%22\)%3B%0A%20%20%20%20%20%20%20%20thread%3A%3Asleep\(Duration%3A%3Afrom_millis\(500\)\)%3B%0A%20%20%20%20%7D%0A%7D%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%2F%2F%20%60Arc%60%20is%20an%20Atomic%20Reference%20Counted%20pointer%20that%20allows%20safe%2C%20shared%20access%0A%2F%2F%20to%20**immutable**%20data.%20But%20we%20want%20to%20*change*%20the%20number%20of%20%60jobs_completed%60%0A%2F%2F%20so%20we%27ll%20need%20to%20also%20use%20another%20type%20that%20will%20only%20allow%20one%20thread%20to%0A%2F%2F%20mutate%20the%20data%20at%20a%20time.%20Take%20a%20look%20at%20this%20section%20of%20the%20book%3A%0A%2F%2F%20https%3A%2F%2Fdoc.rust-lang.org%2Fstable%2Fbook%2Fconcurrency.html%23safe-shared-mutable-state%0A%2F%2F%20and%20keep%20scrolling%20if%20you%27d%20like%20more%20hints%20%3A\)%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%2F%2F%20Do%20you%20now%20have%20an%20%60Arc%60%20%60Mutex%60%20%60JobStatus%60%20at%20the%20beginning%20of%20main%3F%20Like%3A%0A%2F%2F%20%60let%20status%20%3D%20Arc%3A%3Anew\(Mutex%3A%3Anew\(JobStatus%20%7B%20jobs_completed%3A%200%20%7D\)\)%3B%60%0A%2F%2F%20Similar%20to%20the%20code%20in%20the%20example%20in%20the%20book%20that%20happens%20after%20the%20text%0A%2F%2F%20that%20says%20%22We%20can%20use%20Arc%3CT%3E%20to%20fix%20this.%22.%20If%20not%2C%20give%20that%20a%20try!%20If%20you%0A%2F%2F%20do%20and%20would%20like%20more%20hints%2C%20keep%20scrolling!!%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%2F%2F%20Make%20sure%20neither%20of%20your%20threads%20are%20holding%20onto%20the%20lock%20of%20the%20mutex%0A%2F%2F%20while%20they%20are%20sleeping%2C%20since%20this%20will%20prevent%20the%20other%20thread%20from%0A%2F%2F%20being%20allowed%20to%20get%20the%20lock.%20Locks%20are%20automatically%20released%20when%0A%2F%2F%20they%20go%20out%20of%20scope.%0A%0A%2F%2F%20Ok%2C%20so%2C%20real%20talk%2C%20this%20was%20actually%20tricky%20for%20*me*%20to%20do%20too.%20And%0A%2F%2F%20I%20could%20see%20a%20lot%20of%20different%20problems%20you%20might%20run%20into%2C%20so%20at%20this%0A%2F%2F%20point%20I%27m%20not%20sure%20which%20one%20you%27ve%20hit%20%3A\)%20Please%20see%20a%20few%20possible%0A%2F%2F%20answers%20on%20https%3A%2F%2Fgithub.com%2Fcarols10cents%2Frustlings%2Fissues%2F3%20--%0A%2F%2F%20mine%20is%20a%20little%20more%20complicated%20because%20I%20decided%20I%20wanted%20to%20see%0A%2F%2F%20the%20number%20of%20jobs%20currently%20done%20when%20I%20was%20checking%20the%20status.%0A%0A%2F%2F%20Please%20open%20an%20issue%20if%20you%27re%20still%20running%20into%20a%20problem%20that%0A%2F%2F%20these%20hints%20are%20not%20helping%20you%20with%2C%20or%20if%20you%27ve%20looked%20at%20the%20sample%0A%2F%2F%20answers%20and%20don%27t%20understand%20why%20they%20work%20and%20yours%20doesn%27t.%0A%0A%2F%2F%20If%20you%27ve%20learned%20from%20the%20sample%20solutions%2C%20I%20encourage%20you%20to%20come%0A%2F%2F%20back%20to%20this%20exercise%20and%20try%20it%20again%20in%20a%20few%20days%20to%20reinforce%0A%2F%2F%20what%20you%27ve%20learned%20%3A\)%0A&version=stable>)
|
||||
- ["threads1.rs"](http://play.rust-lang.org/?code=%2F%2F+Make+this+compile%21+Scroll+down+for+hints+%3A%29+The+idea+is+the+thread%0A%2F%2F+spawned+on+line+17+is+completing+jobs+while+the+main+thread+is%0A%2F%2F+monitoring+progress+until+10+jobs+are+completed.+If+you+see+6+lines%0A%2F%2F+of+%22waiting...%22+and+the+program+ends+without+timing+out+the+playground%2C%0A%2F%2F+you%27ve+got+it+%3A%29%0A%0Ause+std%3A%3Async%3A%3AArc%3B%0Ause+std%3A%3Athread%3B%0Ause+std%3A%3Atime%3A%3ADuration%3B%0A%0Astruct+JobStatus+%7B%0A++++jobs_completed%3A+u32%2C%0A%7D%0A%0Afn+main%28%29+%7B%0A++++let+status+%3D+Arc%3A%3Anew%28JobStatus+%7B+jobs_completed%3A+0+%7D%29%3B%0A++++let+status_shared+%3D+status.clone%28%29%3B%0A++++thread%3A%3Aspawn%28move+%7C%7C+%7B%0A++++++++for+_+in+0..10+%7B%0A++++++++++++thread%3A%3Asleep%28Duration%3A%3Afrom_millis%28250%29%29%3B%0A++++++++++++status_shared.jobs_completed+%2B%3D+1%3B%0A++++++++%7D%0A++++%7D%29%3B%0A++++while+status.jobs_completed+%3C+10+%7B%0A++++++++println%21%28%22waiting...+%22%29%3B%0A++++++++thread%3A%3Asleep%28Duration%3A%3Afrom_millis%28500%29%29%3B%0A++++%7D%0A%7D%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%2F%2F+%60Arc%60+is+an+Atomic+Reference+Counted+pointer+that+allows+safe%2C+shared+access%0A%2F%2F+to+**immutable**+data.+But+we+want+to+*change*+the+number+of+%60jobs_completed%60%0A%2F%2F+so+we%27ll+need+to+also+use+another+type+that+will+only+allow+one+thread+to%0A%2F%2F+mutate+the+data+at+a+time.+Take+a+look+at+this+section+of+the+book%3A%0A%2F%2F+https%3A%2F%2Fdoc.rust-lang.org%2Fstable%2Fbook%2Fconcurrency.html%23safe-shared-mutable-state%0A%2F%2F+and+keep+scrolling+if+you%27d+like+more+hints+%3A%29%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%2F%2F+Do+you+now+have+an+%60Arc%60+%60Mutex%60+%60JobStatus%60+at+the+beginning+of+main%3F+Like%3A%0A%2F%2F+%60let+status+%3D+Arc%3A%3Anew%28Mutex%3A%3Anew%28JobStatus+%7B+jobs_completed%3A+0+%7D%29%29%3B%60%0A%2F%2F+Similar+to+the+code+in+the+example+in+the+book+that+happens+after+the+text%0A%2F%2F+that+says+%22We+can+use+Arc%3CT%3E+to+fix+this.%22.+If+not%2C+give+that+a+try%21+If+you%0A%2F%2F+do+and+would+like+more+hints%2C+keep+scrolling%21%21%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%2F%2F+Make+sure+neither+of+your+threads+are+holding+onto+the+lock+of+the+mutex%0A%2F%2F+while+they+are+sleeping%2C+since+this+will+prevent+the+other+thread+from%0A%2F%2F+being+allowed+to+get+the+lock.+Locks+are+automatically+released+when%0A%2F%2F+they+go+out+of+scope.%0A%0A%2F%2F+Ok%2C+so%2C+real+talk%2C+this+was+actually+tricky+for+*me*+to+do+too.+And%0A%2F%2F+I+could+see+a+lot+of+different+problems+you+might+run+into%2C+so+at+this%0A%2F%2F+point+I%27m+not+sure+which+one+you%27ve+hit+%3A%29+Please+see+a+few+possible%0A%2F%2F+answers+on+https%3A%2F%2Fgithub.com%2Fcarols10cents%2Frustlings%2Fissues%2F3+--%0A%2F%2F+mine+is+a+little+more+complicated+because+I+decided+I+wanted+to+see%0A%2F%2F+the+number+of+jobs+currently+done+when+I+was+checking+the+status.%0A%0A%2F%2F+Please+open+an+issue+if+you%27re+still+running+into+a+problem+that%0A%2F%2F+these+hints+are+not+helping+you+with%2C+or+if+you%27ve+looked+at+the+sample%0A%2F%2F+answers+and+don%27t+understand+why+they+work+and+yours+doesn%27t.%0A%0A%2F%2F+If+you%27ve+learned+from+the+sample+solutions%2C+I+encourage+you+to+come%0A%2F%2F+back+to+this+exercise+and+try+it+again+in+a+few+days+to+reinforce%0A%2F%2F+what+you%27ve+learned+%3A%29%0A)
|
||||
|
||||
### Uncategorized
|
||||
|
||||
|
|
Loading…
Reference in a new issue