diff --git a/lessons/en/chapter_2.yaml b/lessons/en/chapter_2.yaml index af4974bef..442549d79 100644 --- a/lessons/en/chapter_2.yaml +++ b/lessons/en/chapter_2.yaml @@ -94,6 +94,37 @@ most common patterns you will see in all of Rust. code: >- https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&code=fn%20main()%20%7B%0A%20%20%20%20let%20x%20%3D%2042%3B%0A%0A%20%20%20%20match%20x%20%7B%0A%20%20%20%20%20%20%20%200%20%3D%3E%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20println!(%22found%20zero%22)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%2F%2F%20we%20can%20match%20against%20multiple%20values%0A%20%20%20%20%20%20%20%201%20%7C%202%20%3D%3E%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20println!(%22found%201%20or%202!%22)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%2F%2F%20we%20can%20match%20against%20ranges%0A%20%20%20%20%20%20%20%203..%3D9%20%3D%3E%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20println!(%22found%20a%20number%203%20to%209%20inclusively%22)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%2F%2F%20we%20can%20bind%20the%20matched%20number%20to%20a%20variable%0A%20%20%20%20%20%20%20%20matched_num%20%40%2010..%3D100%20%3D%3E%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20println!(%22found%20%7B%7D%20number%20between%2010%20to%20100!%22%2C%20matched_num)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%2F%2F%20this%20is%20the%20default%20match%20that%20must%20exist%20if%20not%20all%20cases%20are%20handled%0A%20%20%20%20%20%20%20%20_%20%3D%3E%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20println!(%22found%20something%20else!%22)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%7D%0A +- title: Destructuring + content_markdown: > + Destructuring in Rust is the process of breaking down complex + + data structures into their constituent parts. It's a way to match and extract + + specific values from a complex type, making your code more concise and readable. +- title: Destructuring Tuples + content_markdown: > + Deconstructing 'tuples' allows us to extract individual elements from the group. + code: >- + https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&code=fn+main%28%29+%7B%0A++++++++let+point+%3D+%283%2C+4%29%3B%0A%0A++++++++match+point+%7B%0A++++++++++++%2F%2Fchecks+if+the+first+value+is+3%2C+and+prints+the+second+regardless+of+its+value%0A++++++++++++%283%2C+y%29+%3D%3E+%7B%0A++++++++++++println%21%28%22When+the+first+value+is+3%2C+the+second+is+%7B%3A%3F%7D%22%2C+y%29%0A++++++++++++++++%0A++++++++++++%7D%2C%0A++++++++++++%2F%2Fchecks+if+the+last+value+is+4%2C+and+completely+ignores+the+first%0A++++++++++++%28..%2C+4%29+%3D%3E+%7B%0A++++++++++++println%21%28%22The+last+value+is+4%2C+the+others+are+irelevant%22%29%7D%0A++++++++++++%2C%0A++++++++++++%2F%2Fwe+do+not+use+a+default+match%2C+since+the+last+arm+handles+all+cases%0A++++++++++++%28x%2C+y%29+%3D%3E+%7B%0A++++++++++++println%21%28%22The+two+values+are+%7B%3A%3F%7D+and+%7B%3A%3F%7D%22%2C+x+%2C+y%29%0A++++++++++++++++%0A++++++++++++%7D%2C%0A++++++++%7D%0A++++%7D +- title: Destructuring Arrays/Slices + content_markdown: > + 'Arrays' and 'slices' in Rust can be destructured similarly. + code: >- + https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&code=++++fn+main%28%29+%7B%0A++++++++let+numbers+%3D+%5B1%2C+-3%2C+7%2C+4%5D%3B%0A%0A++++++++match+numbers+%7B%0A++++++++++++%2F%2Fmatches+the+first+element%2C+prints+the+second%2C+and+ignores+the+rest%0A++++++++++++%5B1%2C+second%2C+..%5D+%3D%3E+%7B%0A++++++++++++++++println%21%28%22First+two+elements%3A+1%2C+%7B%7D%22%2C+second%29%3B%0A++++++++++++%7D%0A++++++++++++%2F%2Fsingle+values+can+be+ignored+with+%22_%22%2C+larger+groups+utilising+%22..%22%22%0A++++++++++++%5B2%2C+_%2C+third%2C+..%5D+%3D%3E+%7B%0A++++++++++++println%21%28+%22First+is+2%2C+second+we+ignore%2C+third+is+%7B%7D%22%2C+third%29%3B%0A++++++++++++%7D%0A++++++++++++%2F%2Fyou+can+bind+and+store+the+other+elements+in+a+different+array+even%21%0A++++++++++++%5B3%2C+other+%40+..%5D+%3D%3E+%7B%0A++++++++++++println%21%28%0A++++++++++++%22First+is+3%2C+and+the+other+elements+were+%7B%3A%3F%7D%22%2C+other%29%3B%0A++++++++++++%7D%2C%0A++++++++++++_+%3D%3E+%7B%0A++++++++++++++++println%21%28%22Other+cases%22%29%3B%0A++++++++++++%7D%0A++++++++%7D%0A++++%7D +- title: Destructuring Enums + content_markdown: > + 'Enums' require a little more effort, since they have multiple + + fields of different types. + code: >- + https://play.rust-lang.org/?version=beta&mode=debug&edition=2015&code=%2F%2Fthis+allows+us+to+NOT+construct+certain+parts+of+enums%0A%23%5Ballow%28dead_code%29%5D%0Aenum+Cars+%7B%0A++++%2F%2FCar+names+are+Strings%0A++++Mercedes%2C%0A++++WolksWagen%2C%0A++++Opel%2C%0A++++%2F%2Fthe+next+are+fabrication+years%2C+and+the+years+they+changed+owners%0A++++MERC+%28u32%2C+u32%29%2C%0A++++WW+%28u32%2C+u32%2C+u32%2C+u32%29%2C%0A++++OPL+%28u32%2C+u32%2C+u32%29%2C%0A%7D%0A%0Afn+main%28%29+%7B%0A++++let+car_for_sale+%3D+Cars%3A%3AWW+%282006%2C+2011%2C+2014%2C+2019%29%3B%0A++++match+car_for_sale+%7B%0A++++++++%2F%2Fwe+check+to+see+if+the+enum+contains+the+car+make+we+want%0A++++++++Cars%3A%3AWolksWagen+%3D%3E+%7B%0A++++++++++++println%21%28%22There+is+a+WolksWagen+for+sale%21%22%29%3B%0A++++++++%7D%2C%0A++++++++%2F%2Fwe+print+what+year+the+WW+is+made+in%0A++++++++Cars%3A%3AWW%28first_year%2C+..%29+%3D%3E+%7B%0A++++++++++++println%21%28%22The+WW+was+made+in+%7B%7D%22%2C+first_year%29%3B%0A++++++++%7D%2C%0A++++++++%2F%2Fwe+print+the+text+ONLY+IF+the+Opel+was+last+bought+in+2012%0A++++++++Cars%3A%3AOPL%28_%2C+_%2C+2012%29+%3D%3E+%7B%0A++++++++++++println%21%28%22The+last+owner+bought+the+Opel+in+2012.%22%29%3B%0A++++++++%7D%2C%0A++++++++%2F%2Fcovering+other+options%0A++++++++_+%3D%3E+%7B%0A++++++++++++++++println%21%28%22No+WolksWagen+was+for+sale%21%22%29%3B%0A++++++++++++%7D%0A++++%7D%0A%7D +- title: Destructuring Structs + content_markdown: > + 'Structs' DO NOT NEED match blocks to be destructured, but destructuring + + them works on the same logic as those before. + code: >- + https://play.rust-lang.org/?version=beta&mode=debug&edition=2015&code=fn+main%28%29+%7B%0A++++struct+Foo+%7B%0A++++++++x%3A+u32%2C%0A++++++++y%3A+String%2C%0A++++%7D%0A%0A++++%2F%2F+Try+changing+the+values+in+the+struct+to+see+what+happens%0A++++let+foo+%3D+Foo+%7B+x%3A+20%2C+y%3A+String%3A%3Afrom%28%22Foo%22%29+%7D%3B%0A%0A++++match+foo+%7B%0A++++++++%2F%2F+you+can+rename+and+reorder+the+variables%2C%0A++++++++Foo+%7B+y%3A+temp%2C+x%3A+2+%7D+%3D%3E+%7B%0A++++++++++++println%21%28%22The+number+is+2%2C+and+the+String+is+%7B%7D.%22%2C+temp%29%3B%0A++++++++%7D%2C%0A%0A++++++++%2F%2For+you+can+only+account+for+the+variable+you+are+interested+in%0A++++++++Foo+%7B+y%2C+..+%7D+%3D%3E+%7B%0A++++++++println%21%28%22Whatever+the+number+is%2C+the+name+is+%7B%7D.%22%2C+y%29%3B%0A++++++++%7D%2C%0A++++++++%2F%2Fwe+do+not+need+the+default+case%2C+since+the+arm+above+works+for+any+y%0A++++%7D%0A%0A++++%2F%2Fas+mentioned%2C+you+can+destructure+the+struct+without+a+match+block%0A++++let+clone+%3D+Foo+%7B+x%3A+25%2C+y%3A+String%3A%3Afrom%28%22Clone%22%29+%7D%3B%0A++++let+Foo+%7B+x+%3A+temp1%2C+y%3A+temp2+%7D+%3D+clone%3B%0A++++println%21%28%22Number+is+%7Btemp1%7D%2C+and+the+String+is+%7Btemp2%7D.%22%29%3B%0A%7D - title: Returning Values From loop content_markdown: | `loop` can break to return a value. diff --git a/lessons/ro/chapter_2.yaml b/lessons/ro/chapter_2.yaml index 5c4cb5bd4..fdcd88756 100644 --- a/lessons/ro/chapter_2.yaml +++ b/lessons/ro/chapter_2.yaml @@ -92,6 +92,39 @@ cele mai întâlnite șabloane de programare pe care le veți vedea în Rust. code: >- https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&code=fn%20main()%20%7B%0A%20%20%20%20let%20x%20%3D%2042%3B%0A%0A%20%20%20%20match%20x%20%7B%0A%20%20%20%20%20%20%20%200%20%3D%3E%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20println!(%22am%20g%C4%83sit%20zero%22)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%2F%2F%20putem%20face%20un%20caz%20pentru%20mai%20multe%20valori%0A%20%20%20%20%20%20%20%201%20%7C%202%20%3D%3E%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20println!(%22am%20g%C4%83sit%201%20sau%202!%22)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%2F%2F%20putem%20face%20un%20caz%20pentru%20o%20mul%C8%9Bime%0A%20%20%20%20%20%20%20%203..%3D9%20%3D%3E%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20println!(%22am%20g%C4%83sit%20un%20num%C4%83r%20%C3%AEntre%203%20%C8%99i%209%20inclusiv%22)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%2F%2F%20putem%20pune%20num%C4%83rul%20care%20respect%C4%83%20cazul%20%C3%AEntr-o%20variabil%C4%83%0A%20%20%20%20%20%20%20%20matched_num%20%40%2010..%3D100%20%3D%3E%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20println!(%22am%20g%C4%83sit%20num%C4%83rul%20%7B%7D%20%C3%AEntre%2010%20%C8%99i%20100!%22%2C%20matched_num)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%2F%2F%20acesta%20este%20cazul%20implicit%20care%20trebuie%20s%C4%83%20existe%20dac%C4%83%0A%20%20%20%20%20%20%20%20%2F%2F%20nu%20sunt%20abordate%20toate%20cazurile%20posibile%0A%20%20%20%20%20%20%20%20_%20%3D%3E%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20println!(%22am%20g%C4%83sit%20alt%20num%C4%83r!%22)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%7D%0A +title: Destructurarea + content_markdown: > + Destructurarea (destructing) în Rust este procesul de "dezasamblare" a structurilor + + de date în părțile lor individuale. Este un mod de a extrage valori specifice + + din structuri complexe, făcând codul tău mai ușor de urmărit. +- title: Destructurarea Tuplurilor + content_markdown: > + Destructurarea 'tuplurilor' ne permite să izolăm valorile care ne interesează + + din tuplu. + code: >- + https://play.rust-lang.org/?version=beta&mode=debug&edition=2015&code=fn+main%28%29+%7B%0A++++++++let+point+%3D+%283%2C+4%29%3B%0A%0A++++++++match+point+%7B%0A++++++++++++%2F%2Fverific%C4%83+dac%C4%83+prima+valoare+este+3+%C8%99i+o+tip%C4%83re%C8%99te+pe+a+doua%2C+indiferent+de+valoarea+ei.%0A++++++++++++%283%2C+y%29+%3D%3E+%7B%0A++++++++++++println%21%28%22Atunci+c%C3%A2nd+prima+valoare+este+3%2C+a+doua+este+%7B%3A%3F%7D%22%2C+y%29%0A++++++++++++++++%0A++++++++++++%7D%2C%0A++++++++++++%2F%2Fverific%C4%83+dac%C4%83+ultima+valoare+este+4+%C8%99i+ignor%C4%83+complet+prima+valoare.%0A++++++++++++%28..%2C+4%29+%3D%3E+%7B%0A++++++++++++println%21%28%22Ultima+valoare+este+4%2C+restul+sunt+irelevante%22%29%7D%0A++++++++++++%2C%0A++++++++++++%2F%2Fnu+folosim+nici+un+caz+%22default%22+din+moment+ce+ultimul+brat+se+ocupa+de+orice+caz+ramas%0A++++++++++++%28x%2C+y%29+%3D%3E+%7B%0A++++++++++++println%21%28%22Cele+dou%C4%83+valori+sunt+%7B%3A%3F%7D+%C8%99i+%7B%3A%3F%7D%22%2C+x+%2C+y%29%0A++++++++++++++++%0A++++++++++++%7D%2C%0A++++++++%7D%0A++++%7D +- title: Destructurarea Sirurilor/Slice-urilor + content_markdown: > + Sirurile și 'slice'-urile din Rust pot fi destructurate în mod similar. + code: >- + https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&code=++++fn+main%28%29+%7B%0A++++++++let+numbers+%3D+%5B1%2C+-3%2C+7%2C+4%5D%3B%0A%0A++++++++match+numbers+%7B%0A++++++++++++%2F%2Fse+potrive%C8%99te+primul+element%2C+atunci+%C3%AEl+tip%C4%83re%C8%99te+pe+al+doilea+%C8%99i+ignor%C4%83+restul.%0A++++++++++++%5B1%2C+second%2C+..%5D+%3D%3E+%7B%0A++++++++++++++++println%21%28%22Primele+2+elemente+sunt%3A+1%2C+%7B%7D%22%2C+second%29%3B%0A++++++++++++%7D%0A++++++++++++%2F%2Fvalorile+individuale+pot+fi+ignorate+cu+%22_%22%2C+iar+grupurile+mai+mari+cu+%22..%22.%0A++++++++++++%5B2%2C+_%2C+third%2C+..%5D+%3D%3E+%7B%0A++++++++++++println%21%28%22Primul+este+2%2C+pe+al+doilea+%C3%AEl+ignor%C4%83m%2C+al+treilea+este+%7B%7D%22%2C+third%29%3B%0A++++++++++++%7D%0A++++++++++++%2F%2Fpute%C8%9Bi+s%C4%83+lega%C8%9Bi+%C8%99i+s%C4%83+stoca%C8%9Bi+celelalte+elemente+%C3%AEntr-un+array+diferit%21%0A++++++++++++%5B3%2C+other+%40+..%5D+%3D%3E+%7B%0A++++++++++++println%21%28%22Primul+este+3%2C+iar+celelalte+elemente+sunt+%7B%3A%3F%7D%22%2C+other%29%3B%0A++++++++++++%7D%2C%0A++++++++++++_+%3D%3E+%7B%0A++++++++++++++++println%21%28%22Alte+cazuri%22%29%3B%0A++++++++++++%7D%0A++++++++%7D%0A++++%7D +- title: Destructurarea Enums-urilor + content_markdown: > + 'Enums'-urile necesită un efort ceva mai mare, deoarece au mai multe + + câmpuri de tipuri diferite. + code: >- + https://play.rust-lang.org/?version=beta&mode=debug&edition=2015&code=%2F%2Faceast%C4%83+linie+de+cod+ne+permite+s%C4%83+NU+construim+anumite+p%C4%83r%C8%9Bi+ale+enumera%C8%9Biilor%0A%23%5Ballow%28dead_code%29%5D%0Aenum+Cars+%7B%0A++++%2F%2Fnumele+ma%C8%99inilor+sunt+STRING%0A++++Mercedes%2C%0A++++WolksWagen%2C%0A++++Opel%2C%0A++++%2F%2Furm%C4%83torii+sunt+anii+de+fabrica%C8%9Bie+%C8%99i+anii+%C3%AEn+care+au+schimbat+proprietarii%0A++++MERC+%28u32%2C+u32%29%2C%0A++++WW+%28u32%2C+u32%2C+u32%2C+u32%29%2C%0A++++OPL+%28u32%2C+u32%2C+u32%29%2C%0A%7D%0A%0Afn+main%28%29+%7B%0A++++let+car_for_sale+%3D+Cars%3A%3AWW+%282006%2C+2011%2C+2014%2C+2019%29%3B%0A++++match+car_for_sale+%7B%0A++++++++%2F%2Fverific%C4%83m+dac%C4%83+enum-ul+con%C8%9Bine+marca+ma%C8%99inii+pe+care+o+dorim%0A++++++++Cars%3A%3AWolksWagen+%3D%3E+%7B%0A++++++++++++println%21%28%22Este+un+WolksWagen+de+v%C3%A2nzare%21%22%29%3B%0A++++++++%7D%2C%0A++++++++%2F%2Fprint%C4%83m+%C3%AEn+ce+an+este+fabricat+WW%0A++++++++Cars%3A%3AWW%28first_year%2C+..%29+%3D%3E+%7B%0A++++++++++++println%21%28%22WW-ul+a+fost+f%C4%83cut+%C3%AEn+%7B%7D%22%2C+first_year%29%3B%0A++++++++%7D%2C%0A++++++++%2F%2Fimprim%C4%83m+textul+NUMAI+%C3%8EN+CAZUL+%C3%AEn+care+Opel+a+fost+cump%C4%83rat+ultima+dat%C4%83+%C3%AEn+2012%0A++++++++Cars%3A%3AOPL%28_%2C+_%2C+2012%29+%3D%3E+%7B%0A++++++++++++println%21%28%22Ultimul+proprietar+a+cump%C4%83rat+Opel-ul+%C3%AEn+2012.%22%29%3B%0A++++++++%7D%2C%0A++++++++%2F%2Facoperim+celelalte+cazuri%0A++++++++_+%3D%3E+%7B%0A++++++++++++++++println%21%28%22Nici+un+WolksWagen+nu+era+de+v%C3%A2nzare%21%22%29%3B%0A++++++++++++%7D%0A++++%7D%0A%7D +- title: Destructurarea Structurilor + content_markdown: > + Structurile nu au nevoie de blocuri 'match' pentru a fi destructurate, dar destructurarea + + acestora funcționează după aceeași logică ca și cele anterioare. + code: >- + https://play.rust-lang.org/?version=beta&mode=debug&edition=2015&code=fn+main%28%29+%7B%0A++++struct+Foo+%7B%0A++++++++x%3A+u32%2C%0A++++++++y%3A+String%2C%0A++++%7D%0A%0A++++let+foo+%3D+Foo+%7B+x%3A+20%2C+y%3A+String%3A%3Afrom%28%22Foo%22%29+%7D%3B%0A%0A++++match+foo+%7B%0A++++++++%2F%2F+pute%C8%9Bi+redenumi+%C8%99i+reordona+variabilele%2C%0A++++++++Foo+%7B+y%3A+temp%2C+x%3A+2+%7D+%3D%3E+%7B%0A++++++++++++println%21%28%22Num%C4%83rul+este+2%2C+iar+%C8%99irul+este+%7B%7D.%22%2C+temp%29%3B%0A++++++++%7D%2C%0A%0A++++++++%2F%2Fsau+pute%C8%9Bi+lua+%C3%AEn+considerare+doar+variabila+care+v%C4%83+intereseaz%C4%83%0A++++++++Foo+%7B+y%2C+..+%7D+%3D%3E+%7B%0A++++++++println%21%28%22Oricare+ar+fi+num%C4%83rul%2C+numele+este+%7B%7D.%22%2C+y%29%3B%0A++++++++%7D%2C%0A++++++++%2F%2Fnu+avem+nevoie+de+cazul+implicit%2C+deoarece+bra%C8%9Bul+de+mai+sus+func%C8%9Bioneaz%C4%83+pentru+orice+y%0A++++%7D%0A%0A++++%2F%2Fcum+am+men%C8%9Bionat%2C+pute%C8%9Bi+destructura+structura+f%C4%83r%C4%83+un+bloc+de+match%0A++++let+copie+%3D+Foo+%7B+x%3A+25%2C+y%3A+String%3A%3Afrom%28%22Copie%22%29+%7D%3B%0A++++let+Foo+%7B+x+%3A+temp1%2C+y%3A+temp2+%7D+%3D+copie%3B%0A++++println%21%28%22Num%C4%83rul+este+%7Btemp1%7D%2C+iar+%C8%99irul+de+caractere+este+%7Btemp2%7D.%22%29%3B%0A%7D - title: Returnarea unor valori dintr-o buclă content_markdown: | `loop` poate fi oprit pentru a returna o valoare.