From 3c8b042ce52c1cfe61a3e3aabeda47355ed0004c Mon Sep 17 00:00:00 2001 From: mg-chao Date: Wed, 19 Jan 2022 11:33:42 +0800 Subject: [PATCH] update: translate generics --- exercise/exercises/generics/generics1.rs | 6 +++--- exercise/exercises/generics/generics2.rs | 6 +++--- exercise/exercises/generics/generics3.rs | 19 ++++++++----------- exercise/info.toml | 19 +++++++++---------- 4 files changed, 23 insertions(+), 27 deletions(-) diff --git a/exercise/exercises/generics/generics1.rs b/exercise/exercises/generics/generics1.rs index f93e64a0..489aa3e8 100644 --- a/exercise/exercises/generics/generics1.rs +++ b/exercise/exercises/generics/generics1.rs @@ -1,7 +1,7 @@ -// This shopping list program isn't compiling! -// Use your knowledge of generics to fix it. +// 这个购物清单程序无法编译! +// 用你对泛型的了解来解决这个问题。 -// Execute `rustlings hint generics1` for hints! +// 执行 `rustlings hint generics1` 获取提示! // I AM NOT DONE diff --git a/exercise/exercises/generics/generics2.rs b/exercise/exercises/generics/generics2.rs index 1501529c..e329c42d 100644 --- a/exercise/exercises/generics/generics2.rs +++ b/exercise/exercises/generics/generics2.rs @@ -1,7 +1,7 @@ -// This powerful wrapper provides the ability to store a positive integer value. -// Rewrite it using generics so that it supports wrapping ANY type. +// 这个强大的 Wrapper 拥有存储一个正整数值的能力。 +// 利用泛型重写它,使它支持包装(wrapping)任何类型的值。 -// Execute `rustlings hint generics2` for hints! +// 执行 `rustlings hint generics2` 获取提示! // I AM NOT DONE diff --git a/exercise/exercises/generics/generics3.rs b/exercise/exercises/generics/generics3.rs index 64dd9bc1..bf901b83 100644 --- a/exercise/exercises/generics/generics3.rs +++ b/exercise/exercises/generics/generics3.rs @@ -1,14 +1,11 @@ -// An imaginary magical school has a new report card generation system written in Rust! -// Currently the system only supports creating report cards where the student's grade -// is represented numerically (e.g. 1.0 -> 5.5). -// However, the school also issues alphabetical grades (A+ -> F-) and needs -// to be able to print both types of report card! +// 一所想象的魔法学院有一个采用 Rust 编写的新版成绩单生成系统! +// 目前该系统仅支持创建以数字表示的成绩单(如 1.0 -> 5.5)。 +// 然而,学校也发布用字母表示的成绩(A+ -> F-),所以需要能够打印两种成绩单。 -// Make the necessary code changes in the struct ReportCard and the impl block -// to support alphabetical report cards. Change the Grade in the second test to "A+" -// to show that your changes allow alphabetical grades. +// 在 ReportCard 结构定义和 impl 块中进行必要的代码修改,以支持用字母表示的成绩单。 +// 将第二个测试的 grade 改为 "A+",用来表明代码已允许按字母表示成绩。 -// Execute 'rustlings hint generics3' for hints! +// 执行 'rustlings hint generics3' 获取提示! // I AM NOT DONE @@ -20,7 +17,7 @@ pub struct ReportCard { impl ReportCard { pub fn print(&self) -> String { - format!("{} ({}) - achieved a grade of {}", + format!("{} ({}) - achieved a grade of {}",// 译:{} ({}) - 成绩为 {}" &self.student_name, &self.student_age, &self.grade) } } @@ -44,7 +41,7 @@ mod tests { #[test] fn generate_alphabetic_report_card() { - // TODO: Make sure to change the grade here after you finish the exercise. + // TODO:完成练习后,在这里更改 grade 的值。 let report_card = ReportCard { grade: 2.1, student_name: "Gary Plotter".to_string(), diff --git a/exercise/info.toml b/exercise/info.toml index ac72b999..cf6743cd 100644 --- a/exercise/info.toml +++ b/exercise/info.toml @@ -516,18 +516,18 @@ name = "generics1" path = "exercises/generics/generics1.rs" mode = "compile" hint = """ -Vectors in rust make use of generics to create dynamically sized arrays of any type. -You need to tell the compiler what type we are pushing onto this vector.""" +Rust 中的 Vector 通过泛型来创建任意类型的动态数组。 +所以你要告诉编译器,我们要把什么类型的元素推入到这个数组里。""" [[exercises]] name = "generics2" path = "exercises/generics/generics2.rs" mode = "test" hint = """ -Currently we are wrapping only values of type 'u32'. -Maybe we could update the explicit references to this data type somehow? +目前我们只包装了 'u32' 类型的值。 +或许我们可以通过某种方式来修改对类型的显式引用? -If you are still stuck https://doc.rust-lang.org/stable/book/ch10-01-syntax.html#in-method-definitions +如果你还没有头绪:https://doc.rust-lang.org/stable/book/ch10-01-syntax.html#in-method-definitions """ [[exercises]] @@ -535,12 +535,11 @@ name = "generics3" path = "exercises/generics/generics3.rs" mode = "test" hint = """ -To find the best solution to this challenge you're going to need to think back to your -knowledge of traits, specifically Trait Bound Syntax - you may also need this: "use std::fmt::Display;" +为了找到解决这个难题的最佳方法,你需要回想一下有关 trait 的知识,特别是 trait 的绑定语法——可能 +还需要这个:"use std::fmt::Display;" -This is definitely harder than the last two exercises! You need to think about not only making the -ReportCard struct generic, but also the correct property - you will need to change the implementation -of the struct slightly too...you can do it! +这绝对比前两个练习更难!你需要使 ReportCard 结构通用,并拥有正确的属性,同时 +你也需要稍微改变一下结构的实现……加油💪! """ # OPTIONS