178 lines
3.3 KiB
Text
178 lines
3.3 KiB
Text
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Iterators"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "09116649",
|
|
"metadata": {},
|
|
"source": [
|
|
"1. Use iterator methods to sum the values in a vector."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "9fc4b114",
|
|
"metadata": {
|
|
"vscode": {
|
|
"languageId": "rust"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"fn main() {\n",
|
|
" let values = vec![1, 2, 3, 4];\n",
|
|
" let total: i32 = __;\n",
|
|
"\n",
|
|
" assert_eq!(total, 10);\n",
|
|
" println!(\"Success!\");\n",
|
|
"}\n",
|
|
"\n",
|
|
"main();\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "c1558bef",
|
|
"metadata": {},
|
|
"source": [
|
|
"2. Transform each item using `map()` and collect the results into a new vector."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "3a7a9e2b",
|
|
"metadata": {
|
|
"vscode": {
|
|
"languageId": "rust"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"fn main() {\n",
|
|
" let values = vec![1, 2, 3];\n",
|
|
" let doubled: Vec<i32> = __;\n",
|
|
"\n",
|
|
" assert_eq!(doubled, vec![2, 4, 6]);\n",
|
|
" println!(\"Success!\");\n",
|
|
"}\n",
|
|
"\n",
|
|
"main();\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "829e3f48",
|
|
"metadata": {},
|
|
"source": [
|
|
"3. Filter the iterator to keep only even numbers."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "47285545",
|
|
"metadata": {
|
|
"vscode": {
|
|
"languageId": "rust"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"fn main() {\n",
|
|
" let values = vec![1, 2, 3, 4, 5];\n",
|
|
" let evens: Vec<i32> = __;\n",
|
|
"\n",
|
|
" assert_eq!(evens, vec![2, 4]);\n",
|
|
" println!(\"Success!\");\n",
|
|
"}\n",
|
|
"\n",
|
|
"main();\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "545e9a65",
|
|
"metadata": {},
|
|
"source": [
|
|
"4. Use `enumerate()` to find the index of the first value greater than 5."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "622ea110",
|
|
"metadata": {
|
|
"vscode": {
|
|
"languageId": "rust"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"fn main() {\n",
|
|
" let values = vec![2, 4, 6, 8];\n",
|
|
" let idx = __;\n",
|
|
"\n",
|
|
" assert_eq!(idx, Some(2));\n",
|
|
" println!(\"Success!\");\n",
|
|
"}\n",
|
|
"\n",
|
|
"main();"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "fbde588e",
|
|
"metadata": {},
|
|
"source": [
|
|
"5. Calculate sum of `Things` that are even using iterator adaptors\n",
|
|
" "
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "3c3cd0dc",
|
|
"metadata": {
|
|
"vscode": {
|
|
"languageId": "rust"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"#[derive(Clone, Debug, PartialEq, Eq)]\n",
|
|
"struct Thing(u64);\n",
|
|
"\n",
|
|
"let collection: Vec<Thing> = vec![Thing(1), Thing(2), Thing(665), Thing(42), Thing(3), Thing(4)];\n",
|
|
"\n",
|
|
"let result: u64 = __;\n",
|
|
"\n",
|
|
"assert_eq!(result, 48);"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Rust",
|
|
"language": "rust",
|
|
"name": "rust"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": "rust",
|
|
"file_extension": ".rs",
|
|
"mimetype": "text/rust",
|
|
"name": "Rust",
|
|
"pygment_lexer": "rust",
|
|
"version": ""
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|