basic/compound-type/enum #741
Replies: 50 comments 58 replies
-
|
Beta Was this translation helpful? Give feedback.
-
枚举值关联类型的语法好混乱: |
Beta Was this translation helpful? Give feedback.
-
枚举的例子只有声明,没有使用,能否加上 |
Beta Was this translation helpful? Give feedback.
-
null 如果有值,一切正常;null 如果没值,直接崩溃。 简而言之,Rust:“你必须把可能的情况处理掉,这锅我不背” |
Beta Was this translation helpful? Give feedback.
-
好奇的请教下,大小王算哪个花色? |
Beta Was this translation helpful? Give feedback.
-
感觉 |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
如果要使用 println!("{:?}",card); 打印枚举值,需要为枚举类型 PokerSuit 加上#[derive(Debug)] |
Beta Was this translation helpful? Give feedback.
-
提个建议,不要在练习里应到没学的语法,不如在教程里 枚举在数组前面,但是联系里 枚举在数组后面,导致联系里用了数组语法,但是我还没学 |
Beta Was this translation helpful? Give feedback.
-
https://github.com/illicitonion/num_enum #[derive(IntoPrimitive)] fn main() { |
Beta Was this translation helpful? Give feedback.
-
枚举好抽象,,,写点已经理解的
枚举
|
Beta Was this translation helpful? Give feedback.
-
这里的枚举感觉更像是C的union |
Beta Was this translation helpful? Give feedback.
-
看起来rust有好多函数式语言的特性hhh |
Beta Was this translation helpful? Give feedback.
-
类比 Haskell,我感觉 struct 应该类似于使用了 newtype 且用上了 record syntax 的类型;而典型的枚举更像是 sum type,Option 感觉就是对应着(同构于) data Maybe a = Just a | Nothing |
Beta Was this translation helpful? Give feedback.
-
关于枚举类型的内存占用可以了解一下.
rust中的枚举类型, 可以说是Tagged union, 可以分成tag和union两部分:
大小占用 (最终的实际大小并不是 tag + union, 还会有数据对齐等编译优化):
tag和union占用大小都可以是0:
更细节的部分如repr(packed),repr(transparent)会在13.1.4 内存布局 里面讲到. |
Beta Was this translation helpful? Give feedback.
-
这个为啥叫枚举啊,这不就是ADT么,Haskell里面的Data。 |
Beta Was this translation helpful? Give feedback.
-
这章节的课后练习可能不太友好,已经超纲这本书前面所讲的知识点了。建议不会的直接看答案,最后一题可以选择暂时不做。 |
Beta Was this translation helpful? Give feedback.
-
练习题第一题,这个教程里没说枚举值不能是浮点数啊 |
Beta Was this translation helpful? Give feedback.
-
习题的目录和文章目录不太一样,可能会出现超纲的现象 |
Beta Was this translation helpful? Give feedback.
-
花色加携带数字信息的枚举是不是就是结构体方式的语法糖。看定义枚举应该就是一种特殊的整数类型的,现在又能有其他类型的值 |
Beta Was this translation helpful? Give feedback.
-
Option枚举仅有2种情况,Some 和 None , 在使用时通过匹配,显式编写2种情况的处理逻辑。在编译阶段就排除掉错误。 |
Beta Was this translation helpful? Give feedback.
-
some是一个monad? |
Beta Was this translation helpful? Give feedback.
-
有一说一, 我记得在前面说过实现链表是个大坑, 为撒我在课后练习里的第6题看到了链表... |
Beta Was this translation helpful? Give feedback.
-
这个option跟scala中的optional 有点像哦 |
Beta Was this translation helpful? Give feedback.
-
C/C++的枚举里是数字,Java的枚举里是实例,Rust的枚举里像是子类型。 |
Beta Was this translation helpful? Give feedback.
-
这个Option真像js/ts里的可选链啊,防止出现空值程序崩溃。
|
Beta Was this translation helpful? Give feedback.
-
习题超纲 |
Beta Was this translation helpful? Give feedback.
-
#![allow(unused)] //第二种(我写的) //第三种 |
Beta Was this translation helpful? Give feedback.
-
某位同学给的灵感: // 枚举值关联的类型比较混乱
// 可以脑补在前面加一个 struct
enum Message {
Quit,
None(),
Write(String),
ChangeColor(i32, i32, i32),
Move { x: i32, y: i32 },
}
// 如下
struct Quit;
struct None();
struct String;
struct ChangeColor(i32, i32, i32);
struct Move {
x: i32,
y: i32,
} |
Beta Was this translation helpful? Give feedback.
-
感谢大佬的文档,我稍微总结了一下: 为什么 option比空值好?(Option的三大特性)明确性 强制性 可读性 |
Beta Was this translation helpful? Give feedback.
-
basic/compound-type/enum
https://course.rs/basic/compound-type/enum.html
Beta Was this translation helpful? Give feedback.
All reactions