diff --git a/examples/rust.ipynb b/examples/rust.ipynb index cae1c32..dbf8a52 100644 --- a/examples/rust.ipynb +++ b/examples/rust.ipynb @@ -46,11 +46,11 @@ " * @param n 所求数字\n", " * @returns 斐波那契数\n", " */\n", - "fn fib(n: i32) -> i32 {\n", + "fn fib(n: usize) -> usize {\n", " if n < 2 {\n", - " return n;\n", + " n\n", " } else {\n", - " return fib(n - 1) + fib(n - 2);\n", + " fib(n - 1) + fib(n - 2)\n", " }\n", "}" ] @@ -77,19 +77,16 @@ " * @param n 所求数字\n", " * @returns 斐波那契数\n", " */\n", - "fn fib_DP(n:i32) -> i32{\n", - " // 构建线性序列\n", - " // 初始化数组 & 设置初态\n", - " let mut arr: Vec = vec![0, 1];\n", + "fn fib_DP(n: usize) -> usize {\n", + " // 构建线性序列 & 初始化数组 & 设置初态\n", + " let mut arr: Vec = vec![0, 1];\n", " // 设置状态转移方程\n", - " let i: usize; // 只需声明类型,后续在循环中初始化\n", - " let n: usize = n as usize;\n", " for i in 2..(n+1) {\n", " // 状态转移:fib(i>2) => fib(i-1) + fib(i-2)\n", - " arr.push(arr[i - 1] + arr[i - 2]);\n", + " arr.push(arr[i-1] + arr[i-2]);\n", " }\n", " // 返回最终结果\n", - " return arr[n]\n", + " arr[n]\n", "}" ] }, @@ -102,7 +99,7 @@ }, { "cell_type": "code", - "execution_count": 33, + "execution_count": 4, "metadata": { "vscode": { "languageId": "rust" @@ -114,9 +111,15 @@ "output_type": "stream", "text": [ "[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986]\n", - "递归方法: 0.5693288 s\n", + "递归方法: 0.8210494 s\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ "[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986]\n", - "动态规划: 0.0000222 s\n" + "动态规划: 0.0000837 s\n" ] } ], @@ -126,17 +129,19 @@ "use std::time::Instant;\n", "use std::time::Duration;\n", "\n", - "/** 简单的数组打印函数 */\n", - "fn println_vec(v: Vec) {\n", - "\tlet i: usize;\n", - "\tprint!(\"[\");\n", - "\tfor i in 0..v.len() {\n", - "\t\tif i != 0 {\n", - "\t\t\tprint!(\", \");\n", - "\t\t}\n", - "\t\tprint!(\"{}\", v[i])\n", + "/**\n", + " * 根据函数和长度,生成一个指定长度的向量\n", + " * @template T 要生成的向量的类型\n", + " * @param f 函数\n", + " * @param len 长度\n", + " * @return Vec 长度为len、内部元素为`v[i] = f(i)`的向量\n", + " */\n", + "fn generate_vec(f: fn(usize) -> T, len: usize) -> Vec {\n", + "\tlet mut v = Vec::new();\n", + "\tfor i in 0..len {\n", + "\t\tv.push(f(i));\n", "\t}\n", - "\tprintln!(\"]\");\n", + "\tv\n", "}\n", "\n", "/** 打印时间函数 */ \n", @@ -149,19 +154,16 @@ "\n", "/** 定义主函数,允许后续编译成可执行文件 */\n", "fn main() {\n", - "\tconst N: i32 = 40;\n", + "\tconst N: usize = 40;\n", "\n", "\tlet t: Instant = Instant::now();\n", - "\tlet mut arr: Vec = vec![];\n", - "\tfor i in 0..N { arr.push(fib(i)); }\n", - "\tprintln_vec(arr);\n", + "\tlet arr: Vec = generate_vec(fib, N);\n", + "\tprintln!(\"{arr:?}\");\n", "\tprintln_duration_sec(\"递归方法\", t.elapsed());\n", "\t\n", "\tlet t: Instant = Instant::now();\n", - "\tlet mut arr: Vec = vec![];\n", - "\t// arr.clear();\n", - "\tfor i in 0..N { arr.push(fib_DP(i)); }\n", - "\tprintln_vec(arr);\n", + "\tlet arr: Vec = generate_vec(fib_DP, N);\n", + "\tprintln!(\"{arr:?}\");\n", "\tprintln_duration_sec(\"动态规划\", t.elapsed());\n", "}\n", "// %ignore-below\n",