mirror of
https://github.com/rust-lang/rustlings.git
synced 2024-12-26 00:00:03 +03:00
Compare commits
6 commits
83bcce4496
...
ccfcc4f4eb
Author | SHA1 | Date | |
---|---|---|---|
ccfcc4f4eb | |||
26cf4989a2 | |||
6e60f441e9 | |||
d07de879a7 | |||
e59c65cf45 | |||
1c27aeead9 |
83
Cargo.lock
generated
83
Cargo.lock
generated
|
@ -53,9 +53,9 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "anyhow"
|
name = "anyhow"
|
||||||
version = "1.0.93"
|
version = "1.0.94"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "4c95c10ba0b00a02636238b814946408b1322d5ac4760326e6fb8ec956d85775"
|
checksum = "c1fd03a028ef38ba2276dce7e33fcd6369c158a1bca17946c4b1b701891c1ff7"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "autocfg"
|
name = "autocfg"
|
||||||
|
@ -83,9 +83,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "clap"
|
name = "clap"
|
||||||
version = "4.5.20"
|
version = "4.5.23"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "b97f376d85a664d5837dbae44bf546e6477a679ff6610010f17276f686d867e8"
|
checksum = "3135e7ec2ef7b10c6ed8950f0f792ed96ee093fa088608f1c76e569722700c84"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"clap_builder",
|
"clap_builder",
|
||||||
"clap_derive",
|
"clap_derive",
|
||||||
|
@ -93,9 +93,9 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "clap_builder"
|
name = "clap_builder"
|
||||||
version = "4.5.20"
|
version = "4.5.23"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "19bc80abd44e4bed93ca373a0704ccbd1b710dc5749406201bb018272808dc54"
|
checksum = "30582fc632330df2bd26877bde0c1f4470d57c582bbc070376afcd04d8cb4838"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anstream",
|
"anstream",
|
||||||
"anstyle",
|
"anstyle",
|
||||||
|
@ -117,9 +117,9 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "clap_lex"
|
name = "clap_lex"
|
||||||
version = "0.7.2"
|
version = "0.7.4"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "1462739cb27611015575c0c11df5df7601141071f07518d56fcc1be504cbec97"
|
checksum = "f46ad14479a25103f283c0f10005961cf086d8dc42205bb44c46ac563475dca6"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "colorchoice"
|
name = "colorchoice"
|
||||||
|
@ -160,19 +160,19 @@ checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "errno"
|
name = "errno"
|
||||||
version = "0.3.9"
|
version = "0.3.10"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "534c5cf6194dfab3db3242765c03bbe257cf92f22b38f6bc0c58d59108a820ba"
|
checksum = "33d852cb9b869c2a9b3df2f71a3074817f01e1844f839a144f5fcef059a4eb5d"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"libc",
|
"libc",
|
||||||
"windows-sys 0.52.0",
|
"windows-sys 0.59.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "fastrand"
|
name = "fastrand"
|
||||||
version = "2.2.0"
|
version = "2.3.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "486f806e73c5707928240ddc295403b1b93c96a02038563881c4a2fd84b81ac4"
|
checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "filetime"
|
name = "filetime"
|
||||||
|
@ -197,9 +197,9 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "hashbrown"
|
name = "hashbrown"
|
||||||
version = "0.15.1"
|
version = "0.15.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "3a9bfc1af68b1726ea47d3d5109de126281def866b33970e10fbab11b5dafab3"
|
checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "heck"
|
name = "heck"
|
||||||
|
@ -207,17 +207,11 @@ version = "0.5.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
|
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "hermit-abi"
|
|
||||||
version = "0.3.9"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "indexmap"
|
name = "indexmap"
|
||||||
version = "2.6.0"
|
version = "2.7.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "707907fe3c25f5424cce2cb7e1cbcafee6bdbe735ca90ef77c29e84591e5b9da"
|
checksum = "62f822373a4fe84d4bb149bf54e584a7f4abec90e072ed49cda0edea5b95471f"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"equivalent",
|
"equivalent",
|
||||||
"hashbrown",
|
"hashbrown",
|
||||||
|
@ -260,9 +254,9 @@ checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "itoa"
|
name = "itoa"
|
||||||
version = "1.0.11"
|
version = "1.0.14"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b"
|
checksum = "d75a2a4b1b190afb6f5425f10f6a8f959d2ea0b9c2b1d79553551850539e4674"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "kqueue"
|
name = "kqueue"
|
||||||
|
@ -286,9 +280,9 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "libc"
|
name = "libc"
|
||||||
version = "0.2.162"
|
version = "0.2.168"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "18d287de67fe55fd7e1581fe933d965a5a9477b38e949cfa9f8574ef01506398"
|
checksum = "5aaeb2981e0606ca11d79718f8bb01164f1d6ed75080182d3abf017e6d244b6d"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "libredox"
|
name = "libredox"
|
||||||
|
@ -331,11 +325,10 @@ checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "mio"
|
name = "mio"
|
||||||
version = "1.0.2"
|
version = "1.0.3"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "80e04d1dcff3aae0704555fe5fee3bcfaf3d1fdf8a7e521d5b9d2b42acb52cec"
|
checksum = "2886843bf800fba2e3377cff24abf6379b4c4d5c6681eaf9ea5b0d15090450bd"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"hermit-abi",
|
|
||||||
"libc",
|
"libc",
|
||||||
"log",
|
"log",
|
||||||
"wasi",
|
"wasi",
|
||||||
|
@ -411,9 +404,9 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "proc-macro2"
|
name = "proc-macro2"
|
||||||
version = "1.0.89"
|
version = "1.0.92"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "f139b0662de085916d1fb67d2b4169d1addddda1919e696f3252b740b629986e"
|
checksum = "37d3544b3f2748c54e147655edb5025752e2303145b5aefb3c3ea2c78b973bb0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"unicode-ident",
|
"unicode-ident",
|
||||||
]
|
]
|
||||||
|
@ -438,15 +431,15 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rustix"
|
name = "rustix"
|
||||||
version = "0.38.40"
|
version = "0.38.42"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "99e4ea3e1cdc4b559b8e5650f9c8e5998e3e5c1343b4eaf034565f32318d63c0"
|
checksum = "f93dc38ecbab2eb790ff964bb77fa94faf256fd3e73285fd7ba0903b76bedb85"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags 2.6.0",
|
"bitflags 2.6.0",
|
||||||
"errno",
|
"errno",
|
||||||
"libc",
|
"libc",
|
||||||
"linux-raw-sys",
|
"linux-raw-sys",
|
||||||
"windows-sys 0.52.0",
|
"windows-sys 0.59.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -498,18 +491,18 @@ checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "serde"
|
name = "serde"
|
||||||
version = "1.0.214"
|
version = "1.0.215"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "f55c3193aca71c12ad7890f1785d2b73e1b9f63a0bbc353c08ef26fe03fc56b5"
|
checksum = "6513c1ad0b11a9376da888e3e0baa0077f1aed55c17f50e7b2397136129fb88f"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"serde_derive",
|
"serde_derive",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "serde_derive"
|
name = "serde_derive"
|
||||||
version = "1.0.214"
|
version = "1.0.215"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "de523f781f095e28fa605cdce0f8307e451cc0fd14e2eb4cd2e98a355b147766"
|
checksum = "ad1e866f866923f252f05c889987993144fb74e722403468a4ebd70c3cd756c0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
|
@ -518,9 +511,9 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "serde_json"
|
name = "serde_json"
|
||||||
version = "1.0.132"
|
version = "1.0.133"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "d726bfaff4b320266d395898905d0eba0345aae23b54aee3a737e260fd46db03"
|
checksum = "c7fceb2473b9166b2294ef05efcb65a3db80803f0b03ef86a5fc88a2b85ee377"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"itoa",
|
"itoa",
|
||||||
"memchr",
|
"memchr",
|
||||||
|
@ -581,9 +574,9 @@ checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "syn"
|
name = "syn"
|
||||||
version = "2.0.87"
|
version = "2.0.90"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "25aa4ce346d03a6dcd68dd8b4010bcb74e54e62c90c573f394c46eae99aba32d"
|
checksum = "919d3b74a5dd0ccd15aeb8f93e7006bd9e14c295087c9896a110f490752bcf31"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
|
@ -627,9 +620,9 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "unicode-ident"
|
name = "unicode-ident"
|
||||||
version = "1.0.13"
|
version = "1.0.14"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe"
|
checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "utf8parse"
|
name = "utf8parse"
|
||||||
|
|
10
Cargo.toml
10
Cargo.toml
|
@ -19,7 +19,7 @@ edition = "2021" # On Update: Update the edition of the `rustfmt` command that c
|
||||||
rust-version = "1.80"
|
rust-version = "1.80"
|
||||||
|
|
||||||
[workspace.dependencies]
|
[workspace.dependencies]
|
||||||
serde = { version = "1.0.214", features = ["derive"] }
|
serde = { version = "1.0.215", features = ["derive"] }
|
||||||
toml_edit = { version = "0.22.22", default-features = false, features = ["parse", "serde"] }
|
toml_edit = { version = "0.22.22", default-features = false, features = ["parse", "serde"] }
|
||||||
|
|
||||||
[package]
|
[package]
|
||||||
|
@ -46,18 +46,18 @@ include = [
|
||||||
]
|
]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
anyhow = "1.0.93"
|
anyhow = "1.0.94"
|
||||||
clap = { version = "4.5.20", features = ["derive"] }
|
clap = { version = "4.5.23", features = ["derive"] }
|
||||||
crossterm = { version = "0.28.1", default-features = false, features = ["windows", "events"] }
|
crossterm = { version = "0.28.1", default-features = false, features = ["windows", "events"] }
|
||||||
notify = "7.0.0"
|
notify = "7.0.0"
|
||||||
os_pipe = "1.2.1"
|
os_pipe = "1.2.1"
|
||||||
rustlings-macros = { path = "rustlings-macros", version = "=6.4.0" }
|
rustlings-macros = { path = "rustlings-macros", version = "=6.4.0" }
|
||||||
serde_json = "1.0.132"
|
serde_json = "1.0.133"
|
||||||
serde.workspace = true
|
serde.workspace = true
|
||||||
toml_edit.workspace = true
|
toml_edit.workspace = true
|
||||||
|
|
||||||
[target.'cfg(not(windows))'.dependencies]
|
[target.'cfg(not(windows))'.dependencies]
|
||||||
rustix = { version = "0.38.38", default-features = false, features = ["std", "stdio", "termios"] }
|
rustix = { version = "0.38.42", default-features = false, features = ["std", "stdio", "termios"] }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
tempfile = "3.14.0"
|
tempfile = "3.14.0"
|
||||||
|
|
|
@ -26,6 +26,8 @@ bin = [
|
||||||
{ name = "functions4_sol", path = "../solutions/02_functions/functions4.rs" },
|
{ name = "functions4_sol", path = "../solutions/02_functions/functions4.rs" },
|
||||||
{ name = "functions5", path = "../exercises/02_functions/functions5.rs" },
|
{ name = "functions5", path = "../exercises/02_functions/functions5.rs" },
|
||||||
{ name = "functions5_sol", path = "../solutions/02_functions/functions5.rs" },
|
{ name = "functions5_sol", path = "../solutions/02_functions/functions5.rs" },
|
||||||
|
{ name = "functions6", path = "../exercises/02_functions/functions6.rs" },
|
||||||
|
{ name = "functions6_sol", path = "../solutions/02_functions/functions6.rs" },
|
||||||
{ name = "if1", path = "../exercises/03_if/if1.rs" },
|
{ name = "if1", path = "../exercises/03_if/if1.rs" },
|
||||||
{ name = "if1_sol", path = "../solutions/03_if/if1.rs" },
|
{ name = "if1_sol", path = "../solutions/03_if/if1.rs" },
|
||||||
{ name = "if2", path = "../exercises/03_if/if2.rs" },
|
{ name = "if2", path = "../exercises/03_if/if2.rs" },
|
||||||
|
@ -60,6 +62,8 @@ bin = [
|
||||||
{ name = "move_semantics4_sol", path = "../solutions/06_move_semantics/move_semantics4.rs" },
|
{ name = "move_semantics4_sol", path = "../solutions/06_move_semantics/move_semantics4.rs" },
|
||||||
{ name = "move_semantics5", path = "../exercises/06_move_semantics/move_semantics5.rs" },
|
{ name = "move_semantics5", path = "../exercises/06_move_semantics/move_semantics5.rs" },
|
||||||
{ name = "move_semantics5_sol", path = "../solutions/06_move_semantics/move_semantics5.rs" },
|
{ name = "move_semantics5_sol", path = "../solutions/06_move_semantics/move_semantics5.rs" },
|
||||||
|
{ name = "move_semantics6", path = "../exercises/06_move_semantics/move_semantics6.rs" },
|
||||||
|
{ name = "move_semantics6_sol", path = "../solutions/06_move_semantics/move_semantics6.rs" },
|
||||||
{ name = "structs1", path = "../exercises/07_structs/structs1.rs" },
|
{ name = "structs1", path = "../exercises/07_structs/structs1.rs" },
|
||||||
{ name = "structs1_sol", path = "../solutions/07_structs/structs1.rs" },
|
{ name = "structs1_sol", path = "../solutions/07_structs/structs1.rs" },
|
||||||
{ name = "structs2", path = "../exercises/07_structs/structs2.rs" },
|
{ name = "structs2", path = "../exercises/07_structs/structs2.rs" },
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
# Functions
|
# Functions
|
||||||
|
|
||||||
Here, you'll learn how to write functions and how the Rust compiler can help you debug errors even
|
Here, you'll learn how to write functions and how the Rust compiler can help you debug errors even
|
||||||
in more complex code.
|
in more complex code. You will also learn what is the difference with closures.
|
||||||
|
|
||||||
## Further information
|
## Further information
|
||||||
|
|
||||||
- [How Functions Work](https://doc.rust-lang.org/book/ch03-03-how-functions-work.html)
|
- [How Functions Work](https://doc.rust-lang.org/book/ch03-03-how-functions-work.html)
|
||||||
|
- [Closures](https://doc.rust-lang.org/book/ch13-01-closures.html)
|
||||||
|
|
19
exercises/02_functions/functions6.rs
Normal file
19
exercises/02_functions/functions6.rs
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
// functions6.rs
|
||||||
|
//
|
||||||
|
// Here you can practice special functions called `closures`, that can capture
|
||||||
|
// variables of their parent context.
|
||||||
|
// Fix the code below to make it compile, without changing the two closure
|
||||||
|
// definitions.
|
||||||
|
//
|
||||||
|
// Execute `rustlings hint functions6` or use the `hint` watch subcommand for
|
||||||
|
// some hints.
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// TODO: ensure the definition of captured variable
|
||||||
|
let closure_1 = |input_var: u32| -> u32 {input_var + outer_var};
|
||||||
|
println!("Closure#1 returns {}", closure_1(5));
|
||||||
|
|
||||||
|
let closure_2 = |input_var| println!("Closure#2 (input_var {})", input_var);
|
||||||
|
closure_2(2);
|
||||||
|
closure_2("5"); // TODO: look at the captured variable type here
|
||||||
|
}
|
|
@ -19,7 +19,7 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn yummy_food() {
|
fn yummy_food() {
|
||||||
// This means that calling `picky_eater` with the argument "food" should return "Yummy!".
|
// This means that calling `picky_eater` with the argument "strawberry" should return "Yummy!".
|
||||||
assert_eq!(picky_eater("strawberry"), "Yummy!");
|
assert_eq!(picky_eater("strawberry"), "Yummy!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
25
exercises/06_move_semantics/move_semantics6.rs
Normal file
25
exercises/06_move_semantics/move_semantics6.rs
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
// move_semantics6.rs
|
||||||
|
//
|
||||||
|
// Here you will practice how mutable/immutable borrowing works in the context
|
||||||
|
// of a closure.
|
||||||
|
//
|
||||||
|
// Try to fix this code to make it compile and not panic.
|
||||||
|
// You can't change anything except removing 1 line.
|
||||||
|
//
|
||||||
|
// Execute `rustlings hint move_semantics7` or use the `hint` watch subcommand
|
||||||
|
// for a hint.
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let mut counter = 0;
|
||||||
|
|
||||||
|
let mut increment = || {
|
||||||
|
counter += 1;
|
||||||
|
println!("counter equals {}", counter);
|
||||||
|
};
|
||||||
|
|
||||||
|
increment();
|
||||||
|
let _reborrowed_counter = &counter; // TODO: figure out where to put this borrowing instruction
|
||||||
|
increment();
|
||||||
|
|
||||||
|
assert_eq!(counter, 2);
|
||||||
|
}
|
|
@ -3,7 +3,7 @@
|
||||||
| Exercise | Book Chapter |
|
| Exercise | Book Chapter |
|
||||||
| ---------------------- | ------------------- |
|
| ---------------------- | ------------------- |
|
||||||
| variables | §3.1 |
|
| variables | §3.1 |
|
||||||
| functions | §3.3 |
|
| functions | §3.3, §13.1 |
|
||||||
| if | §3.5 |
|
| if | §3.5 |
|
||||||
| primitive_types | §3.2, §4.3 |
|
| primitive_types | §3.2, §4.3 |
|
||||||
| vecs | §8.1 |
|
| vecs | §8.1 |
|
||||||
|
|
|
@ -187,6 +187,20 @@ There are two solutions:
|
||||||
1. Add the `return` keyword before `num * num;`
|
1. Add the `return` keyword before `num * num;`
|
||||||
2. Remove the semicolon `;` after `num * num`"""
|
2. Remove the semicolon `;` after `num * num`"""
|
||||||
|
|
||||||
|
[[exercises]]
|
||||||
|
name = "functions6"
|
||||||
|
dir = "02_functions"
|
||||||
|
test = false
|
||||||
|
hint = """
|
||||||
|
Hint FIX #1: Closures can capture variables defined in the outer context.
|
||||||
|
|
||||||
|
Hint FIX #2: Closures can infer both input and returned types, when they are not
|
||||||
|
specified in the signature. But the closure cannot be reused with different
|
||||||
|
input types.
|
||||||
|
|
||||||
|
Read more about closures in the rust book dedicated section:
|
||||||
|
https://doc.rust-lang.org/book/ch13-01-closures.html"""
|
||||||
|
|
||||||
# IF
|
# IF
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
@ -391,6 +405,18 @@ The first problem is that `get_char` is taking ownership of the string. So
|
||||||
Once you've fixed that, `string_uppercase`'s function signature will also need
|
Once you've fixed that, `string_uppercase`'s function signature will also need
|
||||||
to be adjusted."""
|
to be adjusted."""
|
||||||
|
|
||||||
|
[[exercises]]
|
||||||
|
name = "move_semantics6"
|
||||||
|
dir = "06_move_semantics"
|
||||||
|
test = false
|
||||||
|
hint = """
|
||||||
|
When a closure captures a variable to modify it, it actually borrows that variable
|
||||||
|
as a mutable reference. In this exercise, the closure mutably borrows the `counter`
|
||||||
|
variable, thus, any attempt to borrow `counter` between closure calls leads to an error.
|
||||||
|
|
||||||
|
You cannot immutably borrow a variable if a mutable closure is
|
||||||
|
called later in the scope."""
|
||||||
|
|
||||||
# STRUCTS
|
# STRUCTS
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
|
9
solutions/02_functions/functions6.rs
Normal file
9
solutions/02_functions/functions6.rs
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
fn main() {
|
||||||
|
let outer_var = 1;
|
||||||
|
let closure_1 = |input_var: u32| -> u32 { input_var + outer_var };
|
||||||
|
println!("Closure#1 returns {}", closure_1(5));
|
||||||
|
|
||||||
|
let closure_2 = |input_var| println!("Closure#2 (input_var {})", input_var);
|
||||||
|
closure_2(2);
|
||||||
|
closure_2(5);
|
||||||
|
}
|
14
solutions/06_move_semantics/move_semantics6.rs
Normal file
14
solutions/06_move_semantics/move_semantics6.rs
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
fn main() {
|
||||||
|
let mut counter = 0;
|
||||||
|
|
||||||
|
let mut increment = || {
|
||||||
|
counter += 1;
|
||||||
|
println!("counter equals {}", counter);
|
||||||
|
};
|
||||||
|
|
||||||
|
increment();
|
||||||
|
increment();
|
||||||
|
let _reborrowed_counter = &counter;
|
||||||
|
|
||||||
|
assert_eq!(counter, 2);
|
||||||
|
}
|
Loading…
Reference in a new issue