├── .gitignore ├── LICENSE ├── QA ├── cn │ ├── expert-level │ │ ├── .keep │ │ ├── basic_types │ │ │ ├── anwser │ │ │ │ ├── coding.md │ │ │ │ └── options.md │ │ │ ├── coding.md │ │ │ └── options.md │ │ ├── lifetime │ │ │ ├── anwser │ │ │ │ ├── coding.md │ │ │ │ └── options.md │ │ │ ├── coding.md │ │ │ └── options.md │ │ └── ownership │ │ │ ├── anwser │ │ │ ├── coding.md │ │ │ └── options.md │ │ │ ├── coding.md │ │ │ └── options.md │ ├── quiz-level │ │ ├── .keep │ │ ├── lifetime │ │ │ ├── anwser │ │ │ │ ├── coding.md │ │ │ │ └── options.md │ │ │ ├── coding.md │ │ │ └── options.md │ │ └── ownership │ │ │ ├── anwser │ │ │ ├── coding.md │ │ │ └── options.md │ │ │ ├── coding.md │ │ │ └── options.md │ └── work-level │ │ ├── .keep │ │ ├── lifetime │ │ ├── anwser │ │ │ ├── coding.md │ │ │ └── options.md │ │ ├── coding.md │ │ └── options.md │ │ └── ownership │ │ ├── anwser │ │ ├── coding.md │ │ └── options.md │ │ ├── coding.md │ │ └── options.md └── en │ ├── expert-level │ └── .keep │ ├── quiz-level │ └── .keep │ └── work-level │ └── .keep ├── README.md ├── images ├── 1.png ├── 2.png ├── 3.png └── 4.png └── prompts ├── cn ├── rust-prompt.md └── summary-prompt.md └── en └── prompt.md /.gitignore: -------------------------------------------------------------------------------- 1 | # Generated by Cargo 2 | # will have compiled files and executables 3 | /target/ 4 | 5 | # Remove Cargo.lock from gitignore if creating an executable, leave it for libraries 6 | # More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html 7 | Cargo.lock 8 | 9 | # These are backup files generated by rustfmt 10 | **/*.rs.bk 11 | 12 | .DS_Store 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Alex 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /QA/cn/expert-level/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZhangHanDong/rustchat/0dd3ec4c53744119fac57dab892f08d50c5134d0/QA/cn/expert-level/.keep -------------------------------------------------------------------------------- /QA/cn/expert-level/basic_types/anwser/coding.md: -------------------------------------------------------------------------------- 1 | # 编码实现 2 | 3 | ## 编码实现 1 4 | 5 | ```rust 6 | fn multiply_elements(arr: [i32; 3], n: i32) -> [i32; 3] { 7 | let mut result = [0; 3]; 8 | 9 | for (index, &value) in arr.iter().enumerate() { 10 | result[index] = value * n; 11 | } 12 | 13 | result 14 | } 15 | ``` 16 | 17 | 这个函数首先创建一个新的数组 result,其初始值为 [0; 3]。接着,使用 enumerate() 和 iter() 方法遍历输入数组 arr 中的元素及其索引。对于每个元素,我们将其值乘以 n 并将结果存储在 result 数组的相应位置。最后,返回 result 数组。 -------------------------------------------------------------------------------- /QA/cn/expert-level/basic_types/anwser/options.md: -------------------------------------------------------------------------------- 1 | # 选择题答案 2 | 3 | 4 | ## 答案 5 | 6 | 1. 正确答案:A 7 | 编译成功,输出 "The first word is: hello"。在这个例子中,我们使用 find 方法查找空格的位置,然后创建一个从 0 到 first_word_index 的切片。这样,我们可以得到一个指向原始字符串中第一个单词的切片,然后将其打印出来。 8 | 9 | 2. 正确答案:C 10 | 11 | my_slice 的值是 "st"。在这个例子中,我们创建了一个从索引 5 到索引 7(不包括)的切片。原始字符串的索引 5 和 6 对应的字符分别是 "s" 和 "t",因此切片的值为 "st"。 12 | 13 | 3. 正确答案:A 14 | 15 | 编译成功,输出 "arr1 is now: [4, 5, 6]"。在这个例子中,我们将 arr2 的值赋给了 arr1。由于数组在 Rust 中具有固定大小且这两个数组大小相同,因此这种赋值操作是允许的。 16 | 17 | 4. 正确答案:C 18 | 编译成功,输出 "z: &10"。在这个例子中,我们创建了一个名为 x 的变量,值为 10。然后,我们创建了一个名为 y 的引用,指向 x。最后,我们创建了一个名为 z 的引用,指向 y。因此,z 是指向 x 值的引用的引用。 19 | 20 | 4. 正确答案:D 21 | 编译成功,但运行时会出现未定义行为。虽然可以将 *const i32 转换为 *mut i32,但在这个例子中,x 是不可变的。尝试修改不可变变量会导致未定义行为。 22 | 23 | (注:尝试在 Playground 中使用 Miri 检查 ub) -------------------------------------------------------------------------------- /QA/cn/expert-level/basic_types/coding.md: -------------------------------------------------------------------------------- 1 | # 编码实现题 2 | 3 | 4 | ## 编码实现 1 5 | 6 | 编写一个函数 multiply_elements,它接受一个整数数组 arr 和一个整数 n 作为参数。函数应返回一个新数组,其中每个元素都是原始数组中相应元素的 n 倍。例如,如果 arr = [1, 2, 3] 并且 n = 2,则函数应返回 [2, 4, 6]。 7 | 8 | ```rust 9 | fn multiply_elements(arr: [i32; 3], n: i32) -> [i32; 3] { 10 | // 在这里实现你的代码 11 | } 12 | ``` 13 | -------------------------------------------------------------------------------- /QA/cn/expert-level/basic_types/options.md: -------------------------------------------------------------------------------- 1 | # 选择题 2 | 3 | 4 | ## 选择题1: 5 | 6 | 考虑以下 Rust 代码: 7 | 8 | ```rust 9 | fn main() { 10 | let s = String::from("hello world"); 11 | let first_word_index = s.find(' ').unwrap(); 12 | let slice = &s[0..first_word_index]; 13 | println!("The first word is: {}", slice); 14 | } 15 | ``` 16 | 17 | 这段代码在编译时会出现什么情况? 18 | 19 | A. 编译成功,输出 "The first word is: hello" 20 | B. 编译失败,因为 find 方法返回的是一个 Option 类型 21 | C. 编译失败,因为切片的索引不能用 usize 类型 22 | D. 编译失败,因为 s 的所有权已经被移动到 slice 23 | 24 | 25 | ## 选择题 2 26 | 27 | 以下代码片段定义了一个名为 my_slice 的切片: 28 | 29 | ```rust 30 | let my_str = String::from("Rust is awesome!"); 31 | let my_slice = &my_str[5..7]; 32 | ``` 33 | 34 | 请问 my_slice 的值是什么? 35 | 36 | A. "Rust" 37 | B. "is" 38 | C. "st" 39 | D. "aw" 40 | 41 | ## 选择题 3 42 | 43 | ```rust 44 | fn main() { 45 | let mut arr1 = [1, 2, 3]; 46 | let arr2 = [4, 5, 6]; 47 | 48 | arr1 = arr2; 49 | 50 | println!("arr1 is now: {:?}", arr1); 51 | } 52 | ``` 53 | 54 | 这段代码在编译时会出现什么情况? 55 | 56 | A. 编译成功,输出 "arr1 is now: [4, 5, 6]" 57 | B. 编译失败,因为不能将数组 arr2 直接赋值给数组 arr1 58 | C. 编译失败,因为数组长度不匹配 59 | D. 编译失败,因为数组 arr2 是不可变的 60 | 61 | ## 选择题 4 62 | 63 | ```rust 64 | fn main() { 65 | let x = 10; 66 | let y = &x; 67 | let z = &y; 68 | 69 | println!("z: {}", z); 70 | } 71 | ``` 72 | 73 | 这段代码在编译时会出现什么情况? 74 | 75 | A. 编译成功,输出 "z: 10" 76 | B. 编译失败,因为不能将 y 的引用赋值给 z 77 | C. 编译成功,输出 "z: &10" 78 | D. 编译失败,因为类型不匹配 79 | 80 | ## 选择题 5 81 | 82 | ```rust 83 | fn main() { 84 | let x = 5; 85 | let y = &x as *const i32; 86 | let z = y as *mut i32; 87 | unsafe { 88 | *z += 1; 89 | } 90 | println!("x: {}", x); 91 | } 92 | ``` 93 | 这段代码在编译时会出现什么情况? 94 | 95 | A. 编译成功,输出 "x: 6" 96 | B. 编译失败,因为不能将 *const i32 转换为 *mut i32 97 | C. 编译失败,因为在安全代码中不允许修改 *mut i32 98 | D. 编译成功,但运行时会出现未定义行为 99 | 100 | 101 | -------------------------------------------------------------------------------- /QA/cn/expert-level/lifetime/anwser/coding.md: -------------------------------------------------------------------------------- 1 | # 编码实现 2 | 3 | 4 | 5 | ## 编码实现1 6 | 7 | 要修复这个问题,我们需要确保 result 的生命周期与 s3 的生命周期一致。我们可以将 result 的声明和赋值放在与 s3 相同的作用域内。以下是修复后的代码: 8 | 9 | ```rust 10 | fn longest<'a>(s1: &'a str, s2: &'a str) -> &'a str { 11 | if s1.len() > s2.len() { 12 | s1 13 | } else { 14 | s2 15 | } 16 | } 17 | 18 | fn main() { 19 | let s1 = String::from("short"); 20 | let s2 = String::from("longer"); 21 | 22 | { 23 | let s3 = String::from("longest"); 24 | let result = longest(s2.as_str(), s3.as_str()); 25 | println!("The longest string is {}", result); 26 | } 27 | } 28 | ``` -------------------------------------------------------------------------------- /QA/cn/expert-level/lifetime/anwser/options.md: -------------------------------------------------------------------------------- 1 | # 所有权选择题答案 2 | 3 | 4 | ## 答案 5 | 6 | 1. 正确答案:C 7 | 编译失败,因为 s3 的生命周期在 result 之外。在这个例子中,s3 的生命周期在其所在的作用域内,当这个作用域结束时,s3 将被销毁。因此,在 println! 语句中尝试访问 result 时会导致编译错误,因为 result 指向的字符串已经不再有效。 -------------------------------------------------------------------------------- /QA/cn/expert-level/lifetime/coding.md: -------------------------------------------------------------------------------- 1 | # 编码实现题 2 | 3 | 4 | ## 编码实现 1 5 | 6 | 以下代码在编译时会报错,因为 s3 的生命周期在 result 之外。请修改代码,使其能够正确编译并输出 "The longest string is longest"。 7 | 8 | ```rust 9 | fn longest<'a>(s1: &'a str, s2: &'a str) -> &'a str { 10 | if s1.len() > s2.len() { 11 | s1 12 | } else { 13 | s2 14 | } 15 | } 16 | 17 | fn main() { 18 | let s1 = String::from("short"); 19 | let s2 = String::from("longer"); 20 | 21 | let result; 22 | { 23 | let s3 = String::from("longest"); 24 | result = longest(s2.as_str(), s3.as_str()); 25 | } 26 | println!("The longest string is {}", result); 27 | } 28 | ``` 29 | 30 | -------------------------------------------------------------------------------- /QA/cn/expert-level/lifetime/options.md: -------------------------------------------------------------------------------- 1 | # 选择题 2 | 3 | 4 | ## 选择题1: 5 | 6 | 关于 Rust 的生命周期,以下哪项说法是正确的? 7 | 8 | A. 生命周期是编译时概念,主要用于检查引用的有效性。 9 | B. 生命周期注解只能应用于函数参数,而不能应用于函数返回值。 10 | C. 生命周期会影响程序的运行时性能。 11 | D. 生命周期会导致程序在运行时进行垃圾回收。 12 | 13 | 14 | -------------------------------------------------------------------------------- /QA/cn/expert-level/ownership/anwser/coding.md: -------------------------------------------------------------------------------- 1 | # 编码实现 2 | 3 | ## 编码实现 1 4 | 5 | 要修复这个问题,我们需要避免 s 的所有权被移动到 foo 函数。我们可以将 foo 函数的参数类型更改为 &str,这样它将接受一个字符串切片而非 String 类型。这样,我们可以向 foo 函数传递一个对 s 的不可变引用,而不会移动所有权。以下是修复后的代码: 6 | 7 | ```rust 8 | fn foo(s: &str) -> usize { 9 | s.len() 10 | } 11 | 12 | fn main() { 13 | let s = String::from("hello"); 14 | let length = foo(&s); 15 | println!("The length of '{}' is {}.", s, length); 16 | } 17 | ``` 18 | 19 | 现在,代码可以正确编译并输出 "The length of 'hello' is 5."。 -------------------------------------------------------------------------------- /QA/cn/expert-level/ownership/anwser/options.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZhangHanDong/rustchat/0dd3ec4c53744119fac57dab892f08d50c5134d0/QA/cn/expert-level/ownership/anwser/options.md -------------------------------------------------------------------------------- /QA/cn/expert-level/ownership/coding.md: -------------------------------------------------------------------------------- 1 | # 编码实现题 2 | 3 | 4 | ## 编码实现 1 5 | 6 | 以下代码在编译时会报错,因为在 main 函数中,s 的所有权被移动到了 foo 函数。请修改代码,使其能够正确编译并输出 "The length of 'hello' is 5."。 7 | 8 | ```rust 9 | fn foo(s: String) -> usize { 10 | s.len() 11 | } 12 | 13 | fn main() { 14 | let s = String::from("hello"); 15 | let length = foo(s); 16 | println!("The length of '{}' is {}.", s, length); 17 | } 18 | ``` 19 | 20 | -------------------------------------------------------------------------------- /QA/cn/expert-level/ownership/options.md: -------------------------------------------------------------------------------- 1 | # 选择题 2 | 3 | 4 | ## 选择题1: 5 | 6 | 在以下代码中: 7 | 8 | ```rust 9 | fn foo(s: String) -> usize { 10 | s.len() 11 | } 12 | 13 | fn main() { 14 | let s = String::from("hello"); 15 | let length = foo(s); 16 | println!("The length of '{}' is {}.", s, length); 17 | } 18 | ``` 19 | 20 | 这段代码在编译时会出现什么情况? 21 | 22 | A. 编译成功,输出 "The length of 'hello' is 5." 23 | B. 编译成功,输出 "The length of '' is 5." 24 | C. 编译失败,因为 s 的所有权已经被移动到 foo 函数。 25 | D. 编译失败,因为 String 类型不能用作函数参数。 26 | 27 | -------------------------------------------------------------------------------- /QA/cn/quiz-level/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZhangHanDong/rustchat/0dd3ec4c53744119fac57dab892f08d50c5134d0/QA/cn/quiz-level/.keep -------------------------------------------------------------------------------- /QA/cn/quiz-level/lifetime/anwser/coding.md: -------------------------------------------------------------------------------- 1 | # 编码实现 2 | 3 | 4 | 5 | ## 编码实现1 6 | -------------------------------------------------------------------------------- /QA/cn/quiz-level/lifetime/anwser/options.md: -------------------------------------------------------------------------------- 1 | # 所有权选择题答案 2 | 3 | 4 | ## 答案 5 | 6 | 1. 正确答案:B 7 | 8 | 编译失败,因为 y 的生命周期在 x 之外。在这个例子中,y 在内部作用域中被创建,当这个作用域结束时,y 将被销毁。在 println! 语句中尝试访问 x 时会导致编译错误,因为 x 指向的字符串已经不再有效。 9 | 10 | 2. 正确答案:C 11 | 12 | 编译失败,因为返回值的生命周期不明确。在这个例子中,函数 foo 接受一个生命周期为 'a 的 x 引用和一个没有指定生命周期的 y 引用。然而,函数可能返回 x 或 y 的引用。由于没有为 y 指定生命周期,编译器无法确定返回值的生命周期,从而导致编译失败。要解决这个问题,可以为 y 引用指定与 x 相同的生命周期,如下所示: 13 | 14 | ```rust 15 | fn foo<'a>(x: &'a i32, y: &'a i32) -> &'a i32 { 16 | if *x > *y { 17 | x 18 | } else { 19 | y 20 | } 21 | } 22 | ``` -------------------------------------------------------------------------------- /QA/cn/quiz-level/lifetime/coding.md: -------------------------------------------------------------------------------- 1 | # 编码实现题 2 | 3 | 4 | ## 编码实现 1 5 | 6 | 假设我们有一个结构体 Person,它包含一个名为 name 的 String 字段。现在,请实现一个函数 longest_name,该函数接受两个 Person 的不可变引用,并返回一个指向具有较长名字的 Person 的引用。请考虑生命周期规则,确保函数在编译时不会产生错误。 7 | 8 | ```rust 9 | struct Person { 10 | name: String, 11 | } 12 | 13 | fn longest_name<'a>(p1: &'a Person, p2: &'a Person) -> &'a Person { 14 | // 请在此处实现函数 15 | } 16 | 17 | ``` -------------------------------------------------------------------------------- /QA/cn/quiz-level/lifetime/options.md: -------------------------------------------------------------------------------- 1 | # 选择题 2 | 3 | 4 | ## 选择题1: 5 | 6 | 关于 Rust 的生命周期,以下哪项说法是正确的? 7 | 8 | A. 生命周期是编译时概念,主要用于检查引用的有效性。 9 | B. 生命周期注解只能应用于函数参数,而不能应用于函数返回值。 10 | C. 生命周期会影响程序的运行时性能。 11 | D. 生命周期会导致程序在运行时进行垃圾回收。 12 | 13 | 14 | ## 选择题 2 15 | 16 | 考虑以下 Rust 代码: 17 | 18 | ```rust 19 | fn main() { 20 | let x; 21 | { 22 | let y = String::from("hello"); 23 | x = &y; 24 | } 25 | println!("{}", x); 26 | } 27 | ``` 28 | 29 | 这段代码在编译时会出现什么情况? 30 | 31 | A. 编译成功,输出 "hello" 32 | B. 编译失败,因为 y 的生命周期在 x 之外 33 | C. 编译失败,因为 x 未被初始化 34 | D. 编译失败,因为 y 的所有权已经被移动到 x 35 | 36 | ## 选择题 3 37 | 38 | 考虑以下 Rust 代码: 39 | 40 | ```rust 41 | fn foo<'a>(x: &'a i32, y: &i32) -> &'a i32 { 42 | if *x > *y { 43 | x 44 | } else { 45 | y 46 | } 47 | } 48 | 49 | fn main() { 50 | let x = 5; 51 | let y = 8; 52 | let max = foo(&x, &y); 53 | println!("The maximum value is {}", max); 54 | } 55 | ``` 56 | 57 | 这段代码在编译时会出现什么情况? 58 | 59 | A. 编译成功,输出 "The maximum value is 8" 60 | B. 编译失败,因为 x 和 y 的生命周期不同 61 | C. 编译失败,因为返回值的生命周期不明确 62 | D. 编译失败,因为 y 的引用没有指定生命周期 -------------------------------------------------------------------------------- /QA/cn/quiz-level/ownership/anwser/coding.md: -------------------------------------------------------------------------------- 1 | # 编码实现 2 | 3 | ## 编码实现 1 4 | 5 | ```rust 6 | fn main() { 7 | let s1 = String::from("hello"); 8 | let s2 = s1.clone(); 9 | println!("{}", s1); 10 | } 11 | ``` 12 | 13 | 要修复这个问题,我们需要避免 s1 的所有权被移动到 s2。我们可以通过调用 s1.clone() 来复制 s1 的内容,从而创建一个新的具有相同内容的 String 实例。这样,s1 和 s2 都将拥有各自的所有权,println! 语句可以正确访问 s1,并在屏幕上输出 "hello"。 -------------------------------------------------------------------------------- /QA/cn/quiz-level/ownership/anwser/options.md: -------------------------------------------------------------------------------- 1 | # 所有权选择题答案 2 | 3 | 4 | ## 答案 5 | 6 | 1. 正确答案:C 7 | 8 | 编译失败,因为 s1 的所有权已经被移动到 s2。在 Rust 中,当一个变量被赋值给另一个变量时,前者的所有权会被移动(Move)到后者。在这个例子中,s1 的所有权被移动到了 s2,导致 s1 变得无效。因此,在 println! 语句中尝试访问 s1 时会导致编译错误。 9 | 10 | -------------------------------------------------------------------------------- /QA/cn/quiz-level/ownership/coding.md: -------------------------------------------------------------------------------- 1 | # 编码实现题 2 | 3 | 4 | ## 编码实现 1 5 | 6 | 以下代码在编译时会报错,因为在 main 函数中,s1 的所有权被移动到了 s2。请修改代码,使其能够正确编译并输出 "hello"。 7 | 8 | ```rust 9 | fn main() { 10 | let s1 = String::from("hello"); 11 | let s2 = s1; 12 | println!("{}", s1); 13 | } 14 | ``` -------------------------------------------------------------------------------- /QA/cn/quiz-level/ownership/options.md: -------------------------------------------------------------------------------- 1 | # 选择题 2 | 3 | 4 | 1. 选择题: 5 | 6 | 考虑以下 Rust 代码: 7 | 8 | ```rust 9 | fn main() { 10 | let s1 = String::from("hello"); 11 | let s2 = s1; 12 | println!("{}", s1); 13 | } 14 | ``` 15 | 16 | 这段代码在编译时会出现什么情况? 17 | 18 | A. 编译成功,输出 "hello"。 19 | B. 编译成功,输出 ""。 20 | C. 编译失败,因为 s1 的所有权已经被移动到 s2。 21 | D. 编译失败,因为 String 类型不能用作函数参数。 22 | -------------------------------------------------------------------------------- /QA/cn/work-level/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZhangHanDong/rustchat/0dd3ec4c53744119fac57dab892f08d50c5134d0/QA/cn/work-level/.keep -------------------------------------------------------------------------------- /QA/cn/work-level/lifetime/anwser/coding.md: -------------------------------------------------------------------------------- 1 | # 编码实现 2 | 3 | 4 | 5 | ## 编码实现1 6 | 7 | ```rust 8 | struct Person { 9 | name: String, 10 | } 11 | 12 | fn longest_name<'a>(p1: &'a Person, p2: &'a Person) -> &'a Person { 13 | if p1.name.len() > p2.name.len() { 14 | p1 15 | } else { 16 | p2 17 | } 18 | } 19 | ``` 20 | 21 | 这个实现接受两个具有相同生命周期 'a 的 Person 引用,并返回一个具有相同生命周期的 Person 引用。这样可以确保返回的引用在调用方的上下文中是有效的。函数内部比较 p1.name.len() 和 p2.name.len() 的大小,根据名字的长度返回相应的 Person 引用。 -------------------------------------------------------------------------------- /QA/cn/work-level/lifetime/anwser/options.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZhangHanDong/rustchat/0dd3ec4c53744119fac57dab892f08d50c5134d0/QA/cn/work-level/lifetime/anwser/options.md -------------------------------------------------------------------------------- /QA/cn/work-level/lifetime/coding.md: -------------------------------------------------------------------------------- 1 | # 编码实现题 2 | 3 | 4 | ## 编码实现 1 5 | 6 | 假设我们有一个结构体 Person,它包含一个名为 name 的 String 字段。现在,请实现一个函数 longest_name,该函数接受两个 Person 的不可变引用,并返回一个指向具有较长名字的 Person 的引用。请考虑生命周期规则,确保函数在编译时不会产生错误。 7 | 8 | ```rust 9 | struct Person { 10 | name: String, 11 | } 12 | 13 | fn longest_name<'a>(p1: &'a Person, p2: &'a Person) -> &'a Person { 14 | // 请在此处实现函数 15 | } 16 | 17 | ``` -------------------------------------------------------------------------------- /QA/cn/work-level/lifetime/options.md: -------------------------------------------------------------------------------- 1 | # 选择题 2 | 3 | 4 | ## 选择题1: 5 | 6 | 关于 Rust 的生命周期,以下哪项说法是正确的? 7 | 8 | A. 生命周期是编译时概念,主要用于检查引用的有效性。 9 | B. 生命周期注解只能应用于函数参数,而不能应用于函数返回值。 10 | C. 生命周期会影响程序的运行时性能。 11 | D. 生命周期会导致程序在运行时进行垃圾回收。 12 | 13 | 14 | -------------------------------------------------------------------------------- /QA/cn/work-level/ownership/anwser/coding.md: -------------------------------------------------------------------------------- 1 | # 编码实现答案 2 | 3 | 4 | ## 编码实现 1 5 | 6 | ```rust 7 | fn string_concat(s1: String, s2: String) -> String { 8 | s1 + &s2 9 | } 10 | ``` 11 | 12 | 这个实现利用了 String 类型的 Add trait 实现,将 s1 和 s2 的内容拼接在一起。注意,这里我们将 s2 以不可变引用的形式传递,这样就不会消耗它的所有权,避免了额外的内存拷贝。 -------------------------------------------------------------------------------- /QA/cn/work-level/ownership/anwser/options.md: -------------------------------------------------------------------------------- 1 | # 所有权选择题答案 2 | 3 | 4 | ## 答案 5 | 6 | 1. 正确答案:C 7 | 8 | 所有权系统的目标是在编译时确保内存安全,无需垃圾收集。Rust 的所有权系统可以在编译时检查内存访问的合法性,这样就不需要运行时的垃圾收集机制,从而避免了运行时性能损失。 9 | 10 | 11 | 2. 正确答案:A 12 | 编译成功,输出 "p1: Point { x: 1, y: 2 }"。在这个例子中,我们为 Point 结构体实现了 Debug、Clone 和 Copy trait。因此,当我们将 p1 赋值给 p2 时,p1 的值会被复制而不是移动。所以,我们仍然可以正常访问 p1。 13 | 14 | 3. 正确答案:C 15 | 16 | `(i32, f32)` 类型实现了 `Copy` trait。元组类型实现了 `Copy` trait,如果它的所有元素也实现了 `Copy` trait。在这个例子中,元组的元素类型为 i32 和 f32,它们都实现了 `Copy` trait。因此,`(i32, f32)` 也实现了 `Copy` trait。其他选项中的类型均未实现 Copy trait。 -------------------------------------------------------------------------------- /QA/cn/work-level/ownership/coding.md: -------------------------------------------------------------------------------- 1 | # 编码实现题 2 | 3 | 4 | ## 编码实现题 1 5 | 6 | 请实现一个函数 `string_concat`,该函数接受两个`String` 类型的参数 `s1` 和 `s2`,将它们拼接在一起,并返回拼接后的新字符串。你需要考虑所有权规则,确保函数在编译时不会产生错误。 7 | 8 | ```rust 9 | fn string_concat(s1: String, s2: String) -> String { 10 | // 请在此处实现函数 11 | } 12 | ``` -------------------------------------------------------------------------------- /QA/cn/work-level/ownership/options.md: -------------------------------------------------------------------------------- 1 | # 选择题 2 | 3 | 4 | ## 选择题1: 5 | 6 | 关于 Rust 的所有权系统,以下哪项说法是正确的? 7 | 8 | A. 所有权系统可以确保内存安全,但会导致性能降低。 9 | B. 所有权系统允许一个变量在多个作用域中同时拥有可变引用。 10 | C. 所有权系统的目标是在编译时确保内存安全,无需垃圾收集。 11 | D. 所有权系统仅适用于堆上的内存,不适用于栈上的内存。 12 | 13 | 14 | ## 选择题2 15 | 16 | ```rust 17 | #[derive(Debug, Clone, Copy)] 18 | struct Point { 19 | x: i32, 20 | y: i32, 21 | } 22 | 23 | fn main() { 24 | let p1 = Point { x: 1, y: 2 }; 25 | let p2 = p1; 26 | println!("p1: {:?}", p1); 27 | } 28 | ``` 29 | 这段代码在编译时会出现什么情况? 30 | 31 | A. 编译成功,输出 "p1: Point { x: 1, y: 2 }" 32 | B. 编译失败,因为 Point 类型没有实现 Copy trait 33 | C. 编译失败,因为 Point 类型没有实现 Clone trait 34 | D. 编译失败,因为 Point 类型没有实现 Debug trait 35 | 36 | ## 选择题 3 37 | 38 | 以下哪个类型实现了 Copy trait? 39 | 40 | A. `Vec` 41 | B. `String` 42 | C. `(i32, f32)` 43 | D. `Box` -------------------------------------------------------------------------------- /QA/en/expert-level/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZhangHanDong/rustchat/0dd3ec4c53744119fac57dab892f08d50c5134d0/QA/en/expert-level/.keep -------------------------------------------------------------------------------- /QA/en/quiz-level/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZhangHanDong/rustchat/0dd3ec4c53744119fac57dab892f08d50c5134d0/QA/en/quiz-level/.keep -------------------------------------------------------------------------------- /QA/en/work-level/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZhangHanDong/rustchat/0dd3ec4c53744119fac57dab892f08d50c5134d0/QA/en/work-level/.keep -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # RustChat 2 | 3 | 4 | 5 | ## Move to : https://github.com/Illumine-Labs/RustChat 6 | 7 | 8 | 9 | **Develop GPT as a "magic" system for assisting Rust learning.** 10 | 11 | - [中文说明](https://mp.weixin.qq.com/s/DmsoAJZCWEEwUcoWhGFR9w 12 | ),[在线体验](https://ora.sh/uncertain-lime-98ec/delicate-amber-j2ia) 13 | - [English Description](https://alexzhang-5109.xlog.app/Using-ChatGPT-to-Create-the-Ultimate-Rust-Learning-Magic-System) 14 | 15 | ## Command System Table 16 | 17 | | Main Instruction | Function | 18 | | ---------------- | --------------------------------------------------------- | 19 | | explain | Provides detailed explanations for specified Rust code | 20 | | debug | Helps find code bugs by following given compiler errors | 21 | | work | Outputs questions that cover common Rust features and knowledge for beginners and daily usage | 22 | | expert | Outputs questions that cover Rust expert level features and knowledge | 23 | | quiz | Outputs questions that specifically cover confusing Rust language features | 24 | | anwser | Specifies the number of questions to be answered and provides explanations | 25 | 26 | | First Level Instruction | Function | 27 | | ----------------------- | ---------------------------------------------------------------- | 28 | | -tech | Outputs explanations with relevant examples at the main instruction level | 29 | | -type | Outputs questions at the main instruction level | 30 | | -idea | Outputs project implementation ideas and practical guidance at the main instruction level | 31 | | -crate | Only used with main instruction `work`, recommends relevant third-party crates | 32 | | -n | Only used with main instruction `anwser`, specifies the number of questions to be answered and provides explanations | 33 | 34 | | Second Level Instruction | Function | 35 | | ------------------------ | ----------------------------------------- | 36 | | --cn | Outputs content in Chinese | 37 | | --en | Outputs content in English | 38 | | --jp | Outputs content in Japanese | 39 | 40 | 41 | ## Example 42 | 43 | ![1](./images/1.png) 44 | ![2](./images/2.png) 45 | ![3](./images/3.png) 46 | ![4](./images/4.png) 47 | 48 | 49 | -------------------------------------------------------------------------------- /images/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZhangHanDong/rustchat/0dd3ec4c53744119fac57dab892f08d50c5134d0/images/1.png -------------------------------------------------------------------------------- /images/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZhangHanDong/rustchat/0dd3ec4c53744119fac57dab892f08d50c5134d0/images/2.png -------------------------------------------------------------------------------- /images/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZhangHanDong/rustchat/0dd3ec4c53744119fac57dab892f08d50c5134d0/images/3.png -------------------------------------------------------------------------------- /images/4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZhangHanDong/rustchat/0dd3ec4c53744119fac57dab892f08d50c5134d0/images/4.png -------------------------------------------------------------------------------- /prompts/cn/rust-prompt.md: -------------------------------------------------------------------------------- 1 | # prompt 2 | 3 | 为了确保 GPT-4 可以完整地理解设计的“魔法”,应该采取「整体到细节」的步骤逐步来“喂”它。 4 | 5 | **一、设定主指令** 6 | 7 | ``` 8 | 请你化身为 Rust 专家,来按我的要求帮我生成 Rust 题目,题目默认包括一道选择题和一道编写代码实现的题。默认以中文输出,除非指定了相应的二级指令来告诉你题目数量。 9 | 10 | 题目我打算分为三个级别: 11 | 12 | 1. 工作级别。工作级别问题是覆盖 入门以及日常编写 rust 代码的常用特性和知识。 13 | 2. 专业级别。专业级别覆盖 Rust 专家级特性和知识点。 14 | 3. Quiz 类型。 Quiz 类专门考察容易令人迷惑的 Rust 语言特性的题。 15 | 16 | 主指令对应这三个级别分别为: 17 | 18 | 1. work 指令,对应 工作级别的题目。 19 | 2. expert 指令,对应专业级别题目。 20 | 3. quiz 指令,对应 Quiz 级别题目。 21 | 4. anwser 指令,用来指定应该回答的问题数。 22 | ``` 23 | 24 | 25 | **二、设定二级指令** 26 | 27 | ``` 28 | 接下来介绍一级指令,一级指令是可以和主指令组合使用: 29 | 1. `-tech` ,表示一级指令,代表要对其后指定的内容输出相应主指令级别的概念讲解,并附带示例。 30 | 2. `-type` ,表示一级指令,代表要对其后指定的内容输出相应主指令级别的题目。 31 | 3. `-idea`,表示一级指令,代表要输出对应主指令级别的项目实践想法和思路供人参考。 32 | 4. `-crate`,表示一级指令,代表要对其后指定的内容推荐相应的第三方 crate 33 | 5. `-n` ,表示一级指令, 代表你要给出答案的问题数目,并附上对答案的解释。用到其他主指令的话,就是限制生成问题数量。 34 | ``` 35 | 36 | **三、设定三级指令** 37 | 38 | ``` 39 | 接下来介绍二级指令,二级指令可作用于一级指令后面: 40 | 1. `--cn`,表示二级指令,代表以中文来输出内容 41 | 2. `--en`,表示二级指令,代表以英文来输出内容 42 | 3. `--jp`,表示二级指令,代表以日文来输出内容 43 | ``` 44 | 45 | 四、增加修改指令定义 46 | 47 | 之所以这么费劲地设计这套指令系统,就是为了增加修改指令更加方便。否则,GPT-4 会乱套的。 48 | 49 | 下面是一个修改指令的示例: 50 | 51 | ``` 52 | 修改一级指令: 53 | 54 | `-n` ,表示一级指令, 代表你要给出答案的问题数目,并附上对答案的解释。用到其他主指令的话,就是限制生成问题数量。 55 | 56 | ``` 57 | 58 | 59 | 下面是一个增加指令的示例: 60 | 61 | ``` 62 | 增加主指令: 63 | 64 | explain , 表示对指定的 rust 代码进行详细的解释 65 | debug, 表示按给定的编译器错误提示来帮助查找代码bug并给出修正代码 66 | ``` 67 | 68 | > 说明: explain 本身有解释的意思,所以就算不这样设置主指令,GPT 也能识别这个单词。通常情况下 `explain --cn` 这种方式 GPT 也能识别 cn 并输出中文。 69 | 70 | 使用方式: 71 | 72 | ```rust 73 | debug 74 | 75 | fn add(a: i32, b: i32) -> i32 { 76 | (a + b) as i64 77 | } 78 | 79 | error: 返回类型不匹配 80 | ``` 81 | 82 | # 指令系统表格 83 | 84 | 让 GPT 帮你把所有指令整理成一份表格: 85 | 86 | ``` 87 | 请帮我把刚才设定的主指令,一级指令,二级指令,以及它们的功用,整合为一个markdown表格输出给我 88 | ``` 89 | 90 | 输出如下: 91 | 92 | | 主指令 | 功用 | 93 | | ------- | ----------------------------------------------------- | 94 | | explain | 对指定的 Rust 代码进行详细的解释 | 95 | | debug | 按给定的编译器错误提示来帮助查找代码bug并给出修正代码 | 96 | | work | 输出覆盖 Rust 入门和日常编写的常用特性和知识点的问题 | 97 | | expert | 输出 Rust 专家级特性和知识点的问题 | 98 | | quiz | 输出容易令人迷惑的 Rust 语言特性的题 | 99 | | anwser | 指定应该回答的问题数目,并附上对答案的解释 | 100 | 101 | | 一级指令 | 功用 | 102 | | -------- | -------------------------------------------------------- | 103 | | -tech | 输出相应主指令级别的概念讲解,并附带示例 | 104 | | -type | 输出相应主指令级别的题目 | 105 | | -idea | 输出对应主指令级别的项目实践想法和思路供人参考 | 106 | | -crate | 只限主指令 `work` 使用,推荐相应的第三方 crate | 107 | | -n | 只限主指令 `anwser` 使用,指定要回答的问题数目,并附解释 | 108 | 109 | | 二级指令 | 功用 | 110 | | -------- | ---------------- | 111 | | --cn | 以中文来输出内容 | 112 | | --en | 以英文来输出内容 | 113 | | --jp | 以日文来输出内容 | 114 | -------------------------------------------------------------------------------- /prompts/cn/summary-prompt.md: -------------------------------------------------------------------------------- 1 | # 用于总结和翻译 2 | 3 | 将下面prompt逐步喂给 GPT-4。 4 | 5 | > 注意:GPT-3 不易识别 `con -t` 指令 6 | 7 | ``` 8 | 现在想请你化身为文章总结助手。 9 | 10 | 无论文章是什么语言,默认使用中文进行回复。 11 | 12 | 主指令如下: 13 | 14 | `summary` , 当看见这条指令时,你将对其后输入的文章进行详细的总结,并且使用markdown表格对其中的重点进行梳理汇总。 15 | 16 | `s`,为 `summary` 的简写,功效和 `summary` 相同。 17 | 18 | `trans`,简写为 `t`,当看见这条指令时,你将对其后输入的文章进行完整翻译。默认为中文,除非通过一级指令来指定翻译语言。 19 | 20 | `t`,为 `trans` 的简写,功效和 `trans` 相同。 21 | 22 | ``` 23 | 24 | 增加一级指令: 25 | 26 | ``` 27 | 增加一级指令,可以与主指令组合: 28 | 29 | `-con` ,一级指令,要求你继续完成上一步主指令执行的操作。 30 | ``` 31 | 32 | 使用说明: 33 | 34 | > t -con 剩下的 35 | 36 | 增加二级指令: 37 | 38 | ``` 39 | 增加二级指令如下,二级指令可以和主指令、主指令的简写和一级指令组合使用: 40 | 41 | `--cn` ,表示输出内容要使用中文。 42 | `--en`,表示输出内容要使用英文。 43 | ``` 44 | 45 | 让 GPT 帮你把所有指令整理成一份表格: 46 | 47 | ``` 48 | 请帮我把刚才设定的主指令,一级指令,二级指令,以及它们的功用,整合为一个markdown表格输出给我 49 | ``` 50 | -------------------------------------------------------------------------------- /prompts/en/prompt.md: -------------------------------------------------------------------------------- 1 | # prompt 2 | 3 | To ensure that GPT-4 can fully understand the "magic" of the design, we should adopt a "whole to detail" approach to gradually "feed" it. 4 | 5 | **Step 1: Set up main commands** 6 | 7 | ``` 8 | Please help me generate Rust questions according to my requirements as a Rust expert. By default, each question includes a multiple-choice question and a coding question. The questions will be output in English, unless specified with the corresponding sub-command to indicate the number of questions. 9 | 10 | Questions are divided into three levels: 11 | 12 | 1. Work level. These questions cover the common features and knowledge used in Rust for beginners and daily coding. 13 | 2. Expert level. These questions cover advanced Rust features and knowledge. 14 | 3. Quiz type. These questions are specifically designed to test confusing Rust language features. 15 | 16 | The main commands correspond to these three levels respectively: 17 | 18 | 1. `work` command, corresponding to work-level questions. 19 | 2. `expert` command, corresponding to expert-level questions. 20 | 3. `quiz` command, corresponding to quiz-level questions. 21 | 4. `anwser` command, used to specify the number of questions to be answered. 22 | 23 | ``` 24 | 25 | **Step 2: Set up sub-commands** 26 | 27 | ``` 28 | Next, let's introduce the first-level commands, which can be combined with the main commands: 29 | 1. `-tech`, a sub-command indicating that the content specified after it should output the corresponding main command-level conceptual explanation and be accompanied by examples. 30 | 2. `-type`, a sub-command indicating that the content specified after it should output the corresponding main command-level questions. 31 | 3. `-idea`, a sub-command indicating that the corresponding main command-level project practical ideas and ideas should be output for reference. 32 | 4. `-crate`, a sub-command indicating that the corresponding third-party crate should be recommended for the content specified after it. 33 | 5. `-n`, a sub-command used in conjunction with the `anwser` command, indicating the number of questions to answer and providing explanations. If used with other main commands, it limits the number of questions generated. 34 | ``` 35 | 36 | **Step 3: Set up sub-sub-commands** 37 | 38 | ``` 39 | Next, let's introduce the second-level commands, which can be used after the first-level commands: 40 | 1. `--cn`, a sub-sub-command indicating that the content should be output in Chinese. 41 | 2. `--en`, a sub-sub-command indicating that the content should be output in English. 42 | 3. `--jp`, a sub-sub-command indicating that the content should be output in Japanese. 43 | ``` 44 | 45 | **Step 4: Adding and modifying command definitions** 46 | 47 | The reason for designing this command system so laboriously is to make it more convenient to add and modify commands. Otherwise, GPT-4 will get confused. 48 | 49 | Below is an example of modifying a command: 50 | 51 | ``` 52 | Modify a first-level command: 53 | 54 | `-n`, a sub-command used in conjunction with the `anwser` command, indicating the number of questions to answer and providing explanations. If used with other main commands, it limits the number of questions generated. 55 | ``` 56 | 57 | Here is an example of adding a command: 58 | 59 | ``` 60 | Add a main command: 61 | 62 | `explain`, used to provide detailed explanations for specified Rust code 63 | `debug`, used to help find code bugs according to given compiler error prompts and provide corrected code. 64 | ``` 65 | 66 | > Note: "explain" already has the meaning of "explanation" itself, so even if we don't set it as a main command, GPT can still recognize this word. In most cases, GPT can also recognize "cn" and output Chinese even without setting it as a sub-sub-command. 67 | 68 | Usage example: 69 | 70 | ```rust 71 | debug 72 | 73 | fn add(a: i32, b: i32) -> i32 { 74 | (a + b) as i64 75 | } 76 | 77 | error 78 | 79 | ``` 80 | 81 | 82 | 83 | ## Command System Table 84 | 85 | Ask GPT to help you organize all the commands into a table: 86 | 87 | ``` 88 | Please help me to consolidate the main instructions, the first-level instructions, the second-level instructions, and their functions that I just set into a markdown table. 89 | ``` 90 | 91 | Output: 92 | 93 | | Main Instruction | Function | 94 | | ---------------- | --------------------------------------------------------- | 95 | | explain | Provides detailed explanations for specified Rust code | 96 | | debug | Helps find code bugs by following given compiler errors | 97 | | work | Outputs questions that cover common Rust features and knowledge for beginners and daily usage | 98 | | expert | Outputs questions that cover Rust expert level features and knowledge | 99 | | quiz | Outputs questions that specifically cover confusing Rust language features | 100 | | anwser | Specifies the number of questions to be answered and provides explanations | 101 | 102 | | First Level Instruction | Function | 103 | | ----------------------- | ---------------------------------------------------------------- | 104 | | -tech | Outputs explanations with relevant examples at the main instruction level | 105 | | -type | Outputs questions at the main instruction level | 106 | | -idea | Outputs project implementation ideas and practical guidance at the main instruction level | 107 | | -crate | Only used with main instruction `work`, recommends relevant third-party crates | 108 | | -n | Only used with main instruction `anwser`, specifies the number of questions to be answered and provides explanations | 109 | 110 | | Second Level Instruction | Function | 111 | | ------------------------ | ----------------------------------------- | 112 | | --cn | Outputs content in Chinese | 113 | | --en | Outputs content in English | 114 | | --jp | Outputs content in Japanese | --------------------------------------------------------------------------------