-
Notifications
You must be signed in to change notification settings - Fork 1.1k
basic-types/char-bool-unit #182
-
basic-types/char-bool-unit
Learning Rust By Practice, narrowing the gap between beginner and skilled-dev with challenging examples, exercises and projects.
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 47 -
😄 4 -
🎉 1 -
❤️ 8 -
🚀 4
Replies: 71 comments 23 replies
-
不知道第五行是否少了一个分号,我猜想这应该不是您想考察的地方
fn main() { let _v: () = (); let v = (2, 3); assert_eq!(v, implicitly_ret_unit()) println!("Success!") }
Beta Was this translation helpful? Give feedback.
All reactions
-
❤️ 3
-
是的,少了,应该是因为最后一行是后面复制过去的
Beta Was this translation helpful? Give feedback.
All reactions
-
return (),所以不需要分号
Beta Was this translation helpful? Give feedback.
All reactions
-
只有最后一行不需要分号
Beta Was this translation helpful? Give feedback.
All reactions
-
加上分号返回(),不加分号print!返回也是().加不加分号效果都一样
Beta Was this translation helpful? Give feedback.
All reactions
-
在 Rust 中,语句(Statements)和表达式(Expressions)有一些关键的区别。
- 表达式(Expressions):表达式是计算出某个值的代码片段。例如,
5 + 6
是一个表达式,因为它计算出值11
。函数调用(如println!("Hello, world!")
)和宏调用也是表达式。块{}
,如果它们以表达式结尾,也是表达式。 - 语句(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
,但函数体中没有返回值。
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 10
-
完成 6/6
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1
-
done ! ! !
Beta Was this translation helpful? Give feedback.
All reactions
-
完成!第五题的I will returen...是不是手抖拼错了return😳
Beta Was this translation helpful? Give feedback.
All reactions
-
checked!
Beta Was this translation helpful? Give feedback.
All reactions
-
请问单元类型不占用内存,那么他存放在哪里呢?
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1
-
这里元组是空的所以不占内存, 你试着加几个元素进去再试试看?
Beta Was this translation helpful? Give feedback.
All reactions
-
只是声明,还没有分配内存,声明应该是不放在内存中,因为内存中一般是为了快速运行。目前它是放在你这个文件中,这是我的理解。
Beta Was this translation helpful? Give feedback.
All reactions
-
小白问一下,第一题size_of_val的结果为啥是4,'a'一个字符不是只有一个字节吗
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1
-
重温了知识,字符类型就是4个字节,代表Unicode标量值
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 10
-
没错,字面量是每个4byte,但是字符串不是,1-4任选
Beta Was this translation helpful? Give feedback.
All reactions
-
我需要戴眼镜了🧐,第二题我看半天,还以为char有特殊的输出方式,结果。。。
Beta Was this translation helpful? Give feedback.
All reactions
-
我也看了半天答案...
Beta Was this translation helpful? Give feedback.
All reactions
-
-> 注意,我们还没开始讲字符串,但是这里提前说一下,和一些语言不同,Rust 的字符只能用 '' 来表示, "" 是留给字符串的
我是回去翻的教程
Beta Was this translation helpful? Give feedback.
All reactions
-
这样改也可以:
// 修改一行让代码正常打印 fn main() { let c1 = '中'; print_char(c1); } fn print_char(c : &str) { println!("{}", c); }
Beta Was this translation helpful? Give feedback.
All reactions
-
done!
Beta Was this translation helpful? Give feedback.
All reactions
-
想问下第一题的地方,是不是这个size_of_val()
是因为传入的是一个指针然后这个指针的大小是4B,所以说返回了这个结果?
Beta Was this translation helpful? Give feedback.
All reactions
-
我是傻逼
Beta Was this translation helpful? Give feedback.
All reactions
-
😄 9
-
哈哈哈 🤣
Beta Was this translation helpful? Give feedback.
All reactions
-
Beta Was this translation helpful? Give feedback.
All reactions
-
一般情况下确实256*256两个字节来表示汉字,但是Rust用的是Unicode编码,默认每个字符都是4字节。
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1
-
Beta Was this translation helpful? Give feedback.
All reactions
-
fn main() {
let c1 = '中'; // "" => '' 麻了
print_char(c1);
}
fn print_char(c : char) {
println!("{}", c);
}
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 2
-
lm
Beta Was this translation helpful? Give feedback.
All reactions
-
第5题 implicitly_ret_unit() 返回空元祖吗? ()
Beta Was this translation helpful? Give feedback.
All reactions
-
原文如下哦
例如常见的 println!() 的返回值也是单元类型 ()。
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1 -
🎉 1
-
done
Beta Was this translation helpful? Give feedback.
All reactions
-
Rust 的字符只能用 '' 来表示, "" 是留给字符串的。
字符类型也是占用 4 个字节、布尔值占用内存的大小为 1 个字节、单元类型完全不占用任何内存。
Beta Was this translation helpful? Give feedback.
All reactions
-
Done!
Beta Was this translation helpful? Give feedback.
All reactions
-
DONE!
Beta Was this translation helpful? Give feedback.
All reactions
-
done
Beta Was this translation helpful? Give feedback.
All reactions
-
DONE!
Beta Was this translation helpful? Give feedback.
All reactions
-
println!("Done");
Beta Was this translation helpful? Give feedback.
All reactions
-
day1
Beta Was this translation helpful? Give feedback.
All reactions
-
Done!
Beta Was this translation helpful? Give feedback.
All reactions
-
done
Beta Was this translation helpful? Give feedback.
All reactions
-
I'm done
Beta Was this translation helpful? Give feedback.
All reactions
-
✅
Beta Was this translation helpful? Give feedback.
All reactions
-
Success(6/6)
Beta Was this translation helpful? Give feedback.
All reactions
-
标记完成
Beta Was this translation helpful? Give feedback.
All reactions
-
done
Beta Was this translation helpful? Give feedback.
All reactions
-
done
Beta Was this translation helpful? Give feedback.