Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

basic-types/char-bool-unit #182

giscus[bot] bot announced in Book Comments
Mar 29, 2022 · 71 comments · 23 replies
Discussion options

basic-types/char-bool-unit

Learning Rust By Practice, narrowing the gap between beginner and skilled-dev with challenging examples, exercises and projects.

https://zh.practice.rs/basic-types/char-bool-unit.html

You must be logged in to vote

Replies: 71 comments 23 replies

Comment options

不知道第五行是否少了一个分号,我猜想这应该不是您想考察的地方

fn main() {
 let _v: () = ();
 let v = (2, 3);
 assert_eq!(v, implicitly_ret_unit()) 
 println!("Success!")
}
You must be logged in to vote
5 replies
Comment options

是的,少了,应该是因为最后一行是后面复制过去的

Comment options

return (),所以不需要分号

Comment options

只有最后一行不需要分号

Comment options

加上分号返回(),不加分号print!返回也是().加不加分号效果都一样

Comment options

在 Rust 中,语句(Statements)和表达式(Expressions)有一些关键的区别。

  1. 表达式(Expressions):表达式是计算出某个值的代码片段。例如,5 + 6 是一个表达式,因为它计算出值 11。函数调用(如 println!("Hello, world!"))和宏调用也是表达式。块 {},如果它们以表达式结尾,也是表达式。
  2. 语句(Statements):语句是执行某些操作但不返回值的代码片段。例如,let x = 5; 是一个语句,因为它创建了一个变量 x,但没有返回值。

在 Rust 中,函数体是由一系列语句构成的。如果你想让函数返回一个值,你需要在函数体的最后放一个表达式,而不是语句。这个表达式的值就会成为函数的返回值。注意,这个表达式后面不能有分号,因为在 Rust 中,分号会把表达式转变为语句,而语句不返回值。

例如,以下函数返回一个 i32 类型的值:

fn add_two(x: i32) -> i32 {
	x + 2 // 这是一个表达式,它的值会被返回
}

如果你在这个表达式后面加上分号,它就变成了一个语句,函数就不会返回值了:

fn add_two(x: i32) -> i32 {
 x + 2; // 这是一个语句,它不返回值
}

这段代码会导致编译错误,因为函数声明中指定了返回类型 i32,但函数体中没有返回值。

Comment options

完成 6/6

You must be logged in to vote
1 reply
Comment options

done ! ! !

Comment options

完成!第五题的I will returen...是不是手抖拼错了return😳

You must be logged in to vote
0 replies
Comment options

checked!

You must be logged in to vote
0 replies
Comment options

请问单元类型不占用内存,那么他存放在哪里呢?

You must be logged in to vote
2 replies
Comment options

这里元组是空的所以不占内存, 你试着加几个元素进去再试试看?

Comment options

只是声明,还没有分配内存,声明应该是不放在内存中,因为内存中一般是为了快速运行。目前它是放在你这个文件中,这是我的理解。

Comment options

小白问一下,第一题size_of_val的结果为啥是4,'a'一个字符不是只有一个字节吗

You must be logged in to vote
2 replies
Comment options

重温了知识,字符类型就是4个字节,代表Unicode标量值

Comment options

没错,字面量是每个4byte,但是字符串不是,1-4任选

Comment options

我需要戴眼镜了🧐,第二题我看半天,还以为char有特殊的输出方式,结果。。。

You must be logged in to vote
3 replies
Comment options

我也看了半天答案...

Comment options

-> 注意,我们还没开始讲字符串,但是这里提前说一下,和一些语言不同,Rust 的字符只能用 '' 来表示, "" 是留给字符串的

我是回去翻的教程

Comment options

这样改也可以:

// 修改一行让代码正常打印
fn main() {
 let c1 = '中';
 print_char(c1);
} 
fn print_char(c : &str) {
 println!("{}", c);
}
Comment options

done!

You must be logged in to vote
0 replies
Comment options

想问下第一题的地方,是不是这个size_of_val()
是因为传入的是一个指针然后这个指针的大小是4B,所以说返回了这个结果?

You must be logged in to vote
2 replies
Comment options

我是傻逼

Comment options

哈哈哈 🤣

Comment options

我的理解是汉字是两个byte,一个byte是4bit,两个汉字共16bit
...
---Original--- From: ***@***.***> Date: Wed, Jul 6, 2022 00:08 AM To: ***@***.***>; Cc: ***@***.******@***.***>; Subject: Re: [sunface/rust-by-practice] basic-types/char-bool-unit (Discussion#182) 想问下第一题的地方,是不是这个size_of_val() 是因为传入的是一个指针然后这个指针的大小是4B,所以说返回了这个结果? — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: ***@***.***>
You must be logged in to vote
1 reply
Comment options

一般情况下确实256*256两个字节来表示汉字,但是Rust用的是Unicode编码,默认每个字符都是4字节。

Comment options

额,我应该搞错了,rust字符保存的是unicode,一个汉字是2个unicode共8字节 ,2个汉字共16字节。我也不是很确定
...
---Original--- From: ***@***.***> Date: Wed, Jul 6, 2022 00:11 AM To: ***@***.***>; Cc: ***@***.******@***.***>; Subject: Re: [sunface/rust-by-practice] basic-types/char-bool-unit (Discussion #182) 我是傻逼 — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: ***@***.***>
You must be logged in to vote
0 replies
Comment options

fn main() {
 let c1 = '中'; // "" => '' 麻了
 print_char(c1);
} 
fn print_char(c : char) {
 println!("{}", c);
}
You must be logged in to vote
0 replies
Comment options

lm

You must be logged in to vote
0 replies
Comment options

第5题 implicitly_ret_unit() 返回空元祖吗? ()

You must be logged in to vote
1 reply
Comment options

原文如下哦

例如常见的 println!() 的返回值也是单元类型 ()。
Comment options

done

You must be logged in to vote
0 replies
Comment options

Rust 的字符只能用 '' 来表示, "" 是留给字符串的。
字符类型也是占用 4 个字节、布尔值占用内存的大小为 1 个字节、单元类型完全不占用任何内存。

You must be logged in to vote
0 replies
Comment options

Done!

You must be logged in to vote
0 replies
Comment options

DONE!

You must be logged in to vote
0 replies
Comment options

done

You must be logged in to vote
0 replies
Comment options

DONE!

You must be logged in to vote
0 replies
Comment options

println!("Done");

You must be logged in to vote
0 replies
Comment options

day1

You must be logged in to vote
0 replies
Comment options

Done!

You must be logged in to vote
0 replies
Comment options

done

You must be logged in to vote
0 replies
Comment options

I'm done

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

Success(6/6)

You must be logged in to vote
0 replies
Comment options

标记完成

You must be logged in to vote
0 replies
Comment options

done

You must be logged in to vote
0 replies
Comment options

done

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

AltStyle によって変換されたページ (->オリジナル) /