【 作业打卡 】Week1. 模块一:初识 Rust #4
Replies: 26 comments
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
Week1
|
Beta Was this translation helpful? Give feedback.
-
Di Di 打卡rustup 是什么,如何用它来管理 Rust 版本?rustup 为 rust 的版本管理工具, 类似于 nvm , pyenv 这些工具。 rustup show # 检查安装的版本
rustup install $name # 安装指定版本
rustup uninstall $name # 卸载指定版本
rustup default $name # 设置默认版本,指定版本Rust 中&str 和 String 的区别是什么,每个应该在什么时候使用?str 为定长的字符串,存储在栈上,为rust 原生支持类型。 String 为扩展结构体,本质是一个 vector , 长度可以改变,存储在堆上,同时支持的方法更多。 Rust 中的泛型类型是什么,你可以自己写几个例子吗?泛型为定义结构体,函数,方式的时候,可以传递一个类型参数T。同时,可以实现部分,可以根据不同的类型T 提供不同的实现。可以用来设计更加通用的数据类型。 泛型可以用类设计通用的容器数据结构,内部可以包含多样性的数据。 以下是对Wrapper 的一个简单实现: struct Wrapper<T> {
data: T,
}
impl<T> Wrapper<T> {
fn hello(&self) -> &T {
&self.data
}
fn hello_mut(&mut self) -> &mut T {
&mut self.data
}
}
fn main(){
let mut wrapper = Wrapper {
data: "this in wrapper message ..".to_string(),
};
let data = Wrapper::hello(&wrapper);
println!("{}", data);
let m: &mut String = Wrapper::hello_mut(&mut wrapper);
*m = String::from("this message chagned from outsice ...");
println!("now wrapper message is : {}", wrapper.data);
}Rust 中使用泛型类型的一些常见数据结构有哪些?rust 中容器类型的结构和编码解码类的结构均为泛型结构,以支持更多的数据类型。 比如 vector , hashmap , serde_json 等 Rust 中有哪三种循环结构,它们如何使用?三种循环 :
在 Rust 中,match、while let、let 和 if let 之间的区别是什么,每个应该在什么时候使用?几种应用均可以用于多路选择结构,类似于其他语言里的swich 或者 多重if else 结构。区别在于 match 用户单条结构, while let 是模式匹配和 循环组合使用, if let 是和 选择结构组合使用, let 还可以用于变量结构声明。 Rust 中有哪三种类型的注释?/// 文档注释,一般位于文件开头
// 单行注释
/* */ 组合块状注释 |
Beta Was this translation helpful? Give feedback.
-
Homework11.
|
Beta Was this translation helpful? Give feedback.
-
1-
|
Beta Was this translation helpful? Give feedback.
-
1. rustup 是什么,如何用它来管理 Rust 版本?rustup: a command line tool for managing Rust versions and associated tools. Once Rust is installed via rustup, updating to a newly released version is easy. For example, we can use a command to update rust version via command line. eg: 2. Rust 中&str 和 String 的区别是什么,每个应该在什么时候使用?&str: is "string slice" that refers to a specific sequence of characters in a string. It's an immutable reference, meaning it cannot be changed once bound to a particular string. The difference between &str and String is all about "who owns the memory". How to choose: 3. Rust 中的泛型类型是什么,你可以自己写几个例子吗?Generic is the tool in rust to effectively handle the duplication of concepts/code. Generics abstract stand-ins for concrete types or other properties. With Generic, we can express the behavior of generics or how they relate to other generics without knowing what will be in their place when compiling and running the code. We can define a function signature like above one, the function largest is generic over some type T. 4. Rust 中使用泛型类型的一些常见数据结构有哪些?Generics are widely used in functions, structs, enums, and methods. Example in Enums Example in methods 5. Rust 中有哪三种循环结构,它们如何使用?Rust has three kinds of loops: loop, while, and for. Each of them as following: Conditional Loops with while Looping Through a Collection with for 6. 在 Rust 中,match、while let、let 和 if let 之间的区别是什么,每个应该在什么时候使用?
match vs if let match vs while let Using while let makes this sequence much nicer 7. Rust 中有哪三种类型的注释?i. two slashes comment the whole line. the comment on a separate line above the code it’s annotating ii. Documentation comments use three slashes, ///, instead of two and support Markdown notation for formatting the text. iii. The style of doc comment //! adds documentation to the item that contains the comments rather than to the items following the comments. |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
1、rustup 是什么,如何用它来管理 Rust 版本?rustup是一个管理 Rust 版本和相关工具的命令行工具,可以通过rustup来下载、更新、卸载rust: rustup install xxx 安装rust
rustup update 更新rust
rustup default 切换版本
rustup self uninstall 卸载rustup
rustup toolchain uninstall xxxx卸载对应的rust版本2、Rust 中&str 和 String 的区别是什么,每个应该在什么时候使用?&str是不具有所有权的切片引用,String是具有所有权的字符集合,如果字符串只需要只读操作那么可以优先使用&str,如果字符串需要写的操作那么尽量使用String。 3、Rust 中的泛型类型是什么,你可以自己写几个例子吗?泛型是一种代码复用的抽象,例如让同一个函数处理不同类型的数据: fn add<T: std::ops::Add<Output = T>>(a:T, b:T) -> T {
a + b
}
fn main() {
let a = add(2,3);
println!("{a}");
}如果没有泛型那么i32、i64、u8、u16等等都要单独实现一遍函数,代码量会增加,所以泛型是一种复用的抽象 4、Rust 中使用泛型类型的一些常见数据结构有哪些?vec、&[T]、HashMap<k,v>、VecDeque 5、Rust 中有哪三种循环结构,它们如何使用?// loop 无限循环 loop是表达式
fn main() {
let mut counter = 0;
let result = loop {
counter += 1;
if counter == 10 {
break counter * 2;
}
};
println!("The result is {}", result);
}// while 条件循环 while不是表达式不可以返回值
fn main() {
let mut n = 0;
while n <= 5 {
println!("{}!", n);
n = n + 1;
}
println!("我出来了!");
}// for循环
fn main() {
let a = [4, 3, 2, 1];
// `.iter()` 方法把 `a` 数组变成一个迭代器
for (i, v) in a.iter().enumerate() {
println!("第{}个元素是{}", i + 1, v);
}
}6、在 Rust 中,match、while let、let 和 if let 之间的区别是什么,每个应该在什么时候使用?
7、Rust 中有哪三种类型的注释?// 单行注释 /* */ 多行注释 /// 文档注释 /** */ 文档多行注释 |
Beta Was this translation helpful? Give feedback.
-
rustup 是什么,如何用它来管理 Rust 版本?
Rust 中&str 和 String 的区别是什么,每个应该在什么时候使用?在 Rust 中,
在选择使用
Rust 中的泛型类型是什么,你可以自己写几个例子吗?在 Rust 中,通过泛型,可以编写通用的函数、结构体和枚举,可以在这些实体中使用不特定的类型。 示例:
fn print_item<T>(item: T) {
println!("Item: {:?}", item);
}这个函数
struct Pair<T, U> {
first: T,
second: U,
}这个结构体
impl<T> Pair<T> {
fn new(first: T, second: T) -> Self {
Pair {
first,
second,
}
}
}这个实现块为之前定义的 Rust中使用泛型类型的一些常见数据结构有哪些?
Rust中有哪三种循环结构,它们如何使用?在 Rust 中,有三种主要的循环结构:
在Rust中,match、while let、let和if let之间的区别是什么,每个应该在什么时候使用?在 Rust 中,
总结:
Rust中有哪三种类型的注释?在Rust中,有三种类型的注释:
|
Beta Was this translation helpful? Give feedback.
-
|
感觉课程跳跃性很强,大家基础都挺好的,我还是先看看rust圣经吧。 |
Beta Was this translation helpful? Give feedback.
-
|
1、rustup是什么,如何用它来管理 Rust 版本? 2、Rust中 &str 和 String 的区别是什么,每个应该在什么时候使用? 3、Rust中的泛型类型是什么,你可以自己写几个例子吗? 4、Rust中使用泛型类型的一些常见数据结构有哪些? 5、Rust中有哪三种循环结构,它们如何使用? 6、在Rust中,match、while let、let 和 if let 之间的区别是什么,每个应该在什么时候使用? 7、Rust中有哪三种类型的注释? |
Beta Was this translation helpful? Give feedback.
-
|
1、rustup 是什么,如何用它来管理 Rust 版本? 2、Rust 中&str 和 String 的区别是什么,每个应该在什么时候使用? 3、Rust 中的泛型类型是什么,你可以自己写几个例子吗? 4、Rust 中使用泛型类型的一些常见数据结构有哪些? 5、Rust 中有哪三种循环结构,它们如何使用? 6、在 Rust 中,match、while let、let 和 if let 之间的区别是什么,每个应该在什么时候使用? 7、Rust 中有哪三种类型的注释? fn add(a: i32, b: i32) -> i32 { |
Beta Was this translation helpful? Give feedback.
-
rustup 是 Rust 官方提供的工具,用于安装和管理 不同版本的Rust 。它可以让你在同一台机器上同时拥有多个 Rust 版本,并轻松地在它们之间切换,具体功能类似node的版本管理工具nvm。
// 方法
fn my_fun<T>(value: T) -> T {
println!("my_fun value is {}", value);
value
}
// 结构体
Struct obj<T, U> {
a: T,
b: T,
c: U
}
// 枚举类型
enum Test(T, E) {
OK(T),
Err(E),
}
let vec1 = Vec<u8>::new();
let v2 = vec![1, 2, 3]
let o1:Option<i32> = Some(20);
let o2:Option<i32> = None;
let r1:Result<i32, String> = Ok(100);
let r2:Result<i32, String> = Err("Error");
use std::collections::HashMap;
let mut data:HashMap<String, i32> = HashMap::new();
data.insert("field", 996);
// loop
loop {
println!("Hello");
}
// while
while true {
println!("Hello");
}
// for
for i in [0...=100] {
println!("index -> {}", i);
}
match value {
pattern1 => {
1
}
pattern2 => {
2
}
_ => {
3
}
}
let mut index:i32 = 20;
while let Some(number) = index.checked_sub(1) {
println!("{}", index);
index = number;
}
|
Beta Was this translation helpful? Give feedback.
-
|
1. rustup 是什么,如何用它来管理 Rust 版本? 管理Rust版本以及相关工具的命令行工具,你可以通过它来安装Rust开发环境。 2. Rust 中&str 和 String 的区别是什么,每个应该在什么时候使用?
3. Rust 中的泛型类型是什么,你可以自己写几个例子吗? 泛型类型(Generic types)允许你编写可以在不同类型上进行抽象的代码,从而提高代码的重用性和灵活性。 举例: fn print<T>(value: T) {
println!("{}", value);
}
print(10); // 使用泛型函数打印整数
print("Hello"); // 使用泛型函数打印字符串struct Pair<T, U> {
first: T,
second: U,
}
let pair = Pair { first: 10, second: "Hello" }; // 使用泛型结构体存储不同类型的值
println!("First: {}, Second: {}", pair.first, pair.second);4. Rust 中使用泛型类型的一些常见数据结构有哪些?
5. Rust 中有哪三种循环结构,它们如何使用?
loop {
// 重复执行的代码块
// 在满足某个条件时使用 break 语句退出循环
if condition {
break;
}
}
while condition {
// 在条件为真时重复执行的代码块
}
for item in collection {
// 针对每个元素执行的代码块
}6. 在 Rust 中,match、while let、let 和 if let 之间的区别是什么,每个应该在什么时候使用?
7. Rust 中有哪三种类型的注释?
|
Beta Was this translation helpful? Give feedback.
-
结构体,枚举,函数都可以是泛型类型,
集合vector是由泛型提供支持
|
Beta Was this translation helpful? Give feedback.
-
rustup 是什么,如何用它来管理 Rust 版本?
rustup show # 顯示已安裝或已啟用的工具包
rustup install # 安裝工具包
rustup update # 更新工具版本
rustup self uninstall # 卸載Rust 中&str 和 String 的区别是什么,每个应该在什么时候使用?&str 是不可變字串,是以不可變引用的情況下存在,用在讀取或是擷取substring時 const MY_NAME: &str = "Justa";String 是可變字串,以實體的方式存在 Heap,用在可修改或是要以實體傳入函數時 let mut my_name: String = String::from("Justa");
my_name.push_str(" Liang"); // my_name is now "Justa Liang"Rust 中的泛型类型是什么,你可以自己写几个例子吗?泛型會在編譯的時候帶入型別,善用泛型可以更好地重複利用程式碼。 let num_vec: Vec<i32> = vec![1, 2, 3];
let str_vec: Vec<&str> = vec!["1", "2", "3"];Rust 中使用泛型类型的一些常见数据结构有哪些?泛型在 Rust 中無所不在 Rust 中有哪三种循环结构,它们如何使用?loop loop {
// 重複執行這塊程式
// 可用 break 跳出
}while while condition {
// 在 condition 是 true 的情況下持續執行這塊程式
}for for element in collection {
// 對 collection 裡的每個 element 進行處理
}在 Rust 中,match、while let、let 和 if let 之间的区别是什么,每个应该在什么时候使用?
match pet {
Dog(name) => println!("it's a dog called {}", name),
Cat(name) => println!("it's a cat called {}", name),
Bird(name) => println!("it's a bird called {}", name),
Rabbit(name) => println!("it's a rabbit called {}", name),
}
let x = Option::some(5);
let x = if let Some(num) = x { num } else { 0 }
while let Ok(msg) = returned_msg {
println!("{}", &msg);
}Rust 中有哪三种类型的注释?行註解 // set my name
const MY_NAME: &str = "Justa"段註解 /*
* 1. set the first name
* 2. concat the last name
* */
let mut my_name: String = String::from("Justa");
my_name.push_str(" Liang");文件註解 /// # Examples
///
/// ```
/// let arg = 5;
/// let answer = my_crate::add_one(arg);
///
/// assert_eq!(6, answer);
/// ```
pub fn add_one(x: i32) -> i32 {
x + 1
}文件註解可以輸出標準文件甚至執行裡面的測試 |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
1. rustup 是什么,如何用它来管理 Rust 版本?rustup 是用于管理 Rust,Rust 编译器及其工具的官方工具:
管理Rust版本: 安装指定版本的 Rust 更新 Rust 至最新版本 更新 Rust 至指定版本 切换不同发布通道的 Rust 卸载 Rust 2. Rust 中&str 和 String 的区别是什么,每个应该在什么时候使用?
3. Rust 中的泛型类型是什么,你可以自己写几个例子吗?在Rust中使用泛型generic可以用实现同种功能的结构体/函数/枚举去处理不同类型的参数。 结构体泛型: struct some_struct<T> {
x: T,
y: T,
}函数泛型: fn some_fn<T> (a: T, b: T) -> T {
a + b
}枚举泛型: enum some_enum<T, U> {
one(T),
two(U),
three,
}4. Rust 中使用泛型类型的一些常见数据结构有哪些?
5. Rust 中有哪三种循环结构,它们如何使用?Rust中有 while 循环实现倒数: fn reverse_counter(num: i32){
let mut counter = num;
while counter > 1 {
counter -= 1;
println!("{counter}");
}
}for 循环实现倒数: fn reverse_counter(num: i32){
for i in (1..num).rev() {
println!("{i}");
}
}loop 循环实现倒数: fn reverse_counter(num: i32){
let mut counter = num;
loop {
if counter == 1 { break; }
counter -= 1;
println!("{counter}");
}
}6. 在 Rust 中,match、while let、let 和 if let 之间的区别是什么,每个应该在什么时候使用?
7. Rust 中有哪三种类型的注释?Rust 中有单行注释、多行注释和文档注释。 单行注释: // 这是单行注释多行注释: /*
这是多行注释
*/文档注释: ///
/// 这是文档注释
///
|
Beta Was this translation helpful? Give feedback.
-
fn print<T>(value: T) {
println!("Value: {:?}", value);
}
struct Pair<T, U> {
first: T,
second: U,
}
enum Result<T, E> {
Ok(T),
Err(E),
}
|
Beta Was this translation helpful? Give feedback.
-
|
1.rustup 是什么,如何用它来管理 Rust 版本? 2.Rust 中&str 和 String 的区别是什么,每个应该在什么时候使用?
3.Rust 中的泛型类型是什么,你可以自己写几个例子吗?
4.Rust 中使用泛型类型的一些常见数据结构有哪些?
5.Rust 中有哪三种循环结构,它们如何使用?
6.在 Rust 中,match、while let、let 和 if let 之间的区别是什么,每个应该在什么时候使用?
7.Rust 中有哪三种类型的注释? |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
1. rustup是什么,如何用它来管理Rust版本?
可使用 查看各功能的命令,常见的有更新rust版本,卸载,查看已安装的工具链,切换工具链,等 2. Rust中&str和String的区别是什么,每个应该在什么时候使用?
使用选择:如果只想要一个字符串的只读视图,或者&str作为一个函数的参数,那就首选&str。如果想拥有所有权,想修改字符串那就用String 3. Rust中的泛型类型是什么,你可以自己写几个例子吗?就是一种抽象的复合类型,可以不仅限于一种类型,从而实现一个函数对不同类型参数的复用。比如: 4. Rust中使用泛型类型的一些常见数据结构有哪些?vector,map,struct,eum 5. Rust中有哪三种循环结构,它们如何使用?while,loop,for
6. 在Rust中,match、while let、let和if let之间的区别是什么,每个应该在什么时候使用?
7. Rust中有哪三种类型的注释? |
Beta Was this translation helpful? Give feedback.
-
rustup 是什么,如何用它来管理 Rust 版本?
Rust 中&str 和 String 的区别是什么,每个应该在什么时候使用?
Rust 中的泛型类型是什么,你可以自己写几个例子吗?
Rust中使用泛型类型的一些常见数据结构有哪些?
Rust中有哪三种循环结构,它们如何使用?- forfor <var> in <iterable>{
<loop body>
}
loop {
<loop body>
break;
}
while <condition> {
<loop body>
}在 Rust 中,match、while let、let 和 if let 之间的区别是什么,每个应该在什么时候使用?
Rust 中有哪三种类型的注释?
|
Beta Was this translation helpful? Give feedback.
-
1. rustup 是什么,如何用它来管理 Rust 版本?1) 2. Rust 中&str 和 String 的区别是什么,每个应该在什么时候使用?1)&str(字符串切片):
2)String:
3)选择使用
3. Rust 中的泛型类型是什么,你可以自己写几个例子吗?1)在Rust编程语言中,泛型提供了抽象能力,让代码复用性更强。泛型一般和其它数据结构结合使用。泛型参数一般用大写字母 struct Point<T> {
x: T,
y: T,
}② 泛型枚举 enum Option<T> {
Some(T),
None,
}
enum Result<T, E> {
Ok(T),
Err(E),
}4. Rust 中使用泛型类型的一些常见数据结构有哪些?1)Vec: 5. Rust 中有哪三种循环结构,它们如何使用?在 Rust 中,有三种主要的循环结构,分别是 loop {
// 循环体语句
}
while condition {
// 循环体语句
}
for item in iterator {
// 循环体语句
}
6. 在 Rust 中,match、while let、let 和 if let 之间的区别是什么,每个应该在什么时候使用?在 Rust 中, 7. Rust 中有哪三种类型的注释?1)文档注释 /// 1. 文档注释,一般写在当前文件的最顶端
2)多行注释 /*
2. 多行注释
*/3)单行注释 // 3. 单行注释 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Beta Was this translation helpful? Give feedback.
All reactions