知识卡片
常量要求编译期值
const 为编译期能够求出的值起名,须写明类型,且没有 mut。运行时才知道的环境输入适合普通绑定。将复用的上限或单位换算系数写成常量表达式,可以让定义和使用位置都保留领域含义。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 常见编程概念 Variables and Mutability · 查看原书依据
First, you aren’t allowed to use mut with constants. Constants aren’t just immutable by default—they’re always immutable. You declare constants using the const keyword instead of the let keyword, and the type of the value must be annotated. We’ll cover types and type annotations in “Data Types” on page 36, so don’t worry about the details right now. Just know that you must always annotate the type.
参考来源
- 位置:《The Rust Programming Language(第2版)》第03章 常见编程概念(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c03.xhtml`,块 0027)
- 结论依据:书中列出 const 的类型标注要求,并把它与普通绑定区分。
- 原始内容:First, you aren’t allowed to use mut with constants. Constants aren’t just immutable by default—they’re always immutable. You declare constants using the const keyword instead of the let keyword, and the type of the value must be annotated. We’ll cover types and type annotations in “Data Types” on page 36, so don’t worry about the details right now. Just know that you must always annotate the type.