trainings/Rust_2026/rust-bs-2026-09-14/exercises/rust-exercises/8_generics.ipynb

202 lines
4.1 KiB
Text

{
"cells": [
{
"cell_type": "markdown",
"id": "19ddc3b6",
"metadata": {},
"source": [
"# Generics\n",
"\n",
"## Functions"
]
},
{
"cell_type": "markdown",
"id": "dc4012b9",
"metadata": {},
"source": [
"1."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1f850fa6",
"metadata": {
"vscode": {
"languageId": "rust"
}
},
"outputs": [],
"source": [
"\n",
"// Fill in the blanks to make it work\n",
"struct A; // Concrete type `A`.\n",
"struct S(A); // Concrete type `S`.\n",
"struct SGen<T>(T); // Generic type `SGen`.\n",
"\n",
"fn reg_fn(_s: S) {}\n",
"\n",
"fn gen_spec_t(_s: SGen<A>) {}\n",
"\n",
"fn gen_spec_i32(_s: SGen<i32>) {}\n",
"\n",
"fn generic<T>(_s: SGen<T>) {}\n",
"\n",
"fn main() {\n",
" // Using the non-generic functions\n",
" reg_fn(__); // Concrete type.\n",
" gen_spec_t(__); // Implicitly specified type parameter `A`.\n",
" gen_spec_i32(__); // Implicitly specified type parameter `i32`.\n",
"\n",
" // Explicitly specified type parameter `char` to `generic()`.\n",
" generic::<char>(__);\n",
"\n",
" // Implicitly specified type parameter `char` to `generic()`.\n",
" generic(__);\n",
"\n",
" println!(\"Success!\");\n",
"}\n",
"\n",
"main();"
]
},
{
"cell_type": "markdown",
"id": "584e4753",
"metadata": {},
"source": [
"2."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2f005be9",
"metadata": {
"vscode": {
"languageId": "rust"
}
},
"outputs": [],
"source": [
"// Implement the generic function below.\n",
"fn sum\n",
"\n",
"fn main() {\n",
" assert_eq!(5, sum(2i8, 3i8));\n",
" assert_eq!(50, sum(20, 30));\n",
" assert_eq!(2.46, sum(1.23, 1.23));\n",
"\n",
" println!(\"Success!\");\n",
"}\n",
"\n",
"main();"
]
},
{
"cell_type": "markdown",
"id": "ad822adf",
"metadata": {},
"source": [
"## Struct and impl"
]
},
{
"cell_type": "markdown",
"id": "a3df7fa0",
"metadata": {},
"source": [
"3."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "52333cde",
"metadata": {
"vscode": {
"languageId": "rust"
}
},
"outputs": [],
"source": [
"// Implement struct Point to make it work.\n",
"\n",
"fn main() {\n",
" let pt_integer = Point { x: 5, y: 10 };\n",
" println!(\"pt_integer: {:?}\", pt_integer);\n",
" assert_eq!(pt_integer.x(), 5);\n",
" assert_eq!(pt_integer.y(), 10);\n",
"\n",
" let pt_float = Point { x: 1.0, y: 4.0 };\n",
" println!(\"pt_float: {:?}\", pt_float);\n",
" assert_eq!(pt_float.x(), 1.0);\n",
" assert_eq!(pt_float.y(), 4.0);\n",
"\n",
" println!(\"Success!\");\n",
"}\n",
"\n",
"main();"
]
},
{
"cell_type": "markdown",
"id": "a1621757",
"metadata": {},
"source": [
"4."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0e5292ca",
"metadata": {
"vscode": {
"languageId": "rust"
}
},
"outputs": [],
"source": [
"// Fix the errors to make the code work.\n",
"struct Point<T> {\n",
" x: T,\n",
" y: T,\n",
"}\n",
"\n",
"impl Point<f32> {\n",
" fn distance_from_origin(&self) -> f32 {\n",
" (self.x.powi(2) + self.y.powi(2)).sqrt()\n",
" }\n",
"}\n",
"\n",
"fn main() {\n",
" let p = Point{x: 5, y: 10};\n",
" println!(\"{}\",p.distance_from_origin());\n",
"\n",
" println!(\"Success!\");\n",
"}\n",
"\n",
"main();"
]
}
],
"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
}