知识卡片
函数签名是类型契约
给函数参数标注类型,让编译器在函数边界知道允许的输入。函数声明可位于调用之后,只要调用处有权访问它。调用传入实参,函数体通过参数名使用相应值。对接口显式表达类型也让诊断能指出具体不匹配。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 常见编程概念 Functions · 查看原书依据
In function signatures, you must declare the type of each parameter. This is a deliberate decision in Rust’s design: requiring type annotations in function definitions means the compiler almost never needs you to use them elsewhere in the code to figure out what type you mean. The compiler is also able to give more helpful error messages if it knows what types the function expects.
参考来源
- 位置:《The Rust Programming Language(第2版)》第03章 常见编程概念(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c03.xhtml`,块 0170)
- 结论依据:原文给出显式参数类型对推断与诊断的作用。
- 原始内容:In function signatures, you must declare the type of each parameter. This is a deliberate decision in Rust’s design: requiring type annotations in function definitions means the compiler almost never needs you to use them elsewhere in the code to figure out what type you mean. The compiler is also able to give more helpful error messages if it knows what types the function expects.