什么时候在Rust中进行类型绑定? [英] When does type binding happen in Rust?

查看:105
本文介绍了什么时候在Rust中进行类型绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知:在C语言中,变量的类型在编译时绑定,而该变量的值在运行时绑定。

From what I know: In C language, the "type" of a variable is bound during compile time and the value of that variable is bound during run time.

例如,在 int a = 10; 中,类型 int 在编译期间绑定到变量 a ,并且实际值 10 绑定(或分配)

For example, in int a = 10;, the type int is bound to the variable a during compile time and the actual value 10 is bound (or assigned) to it during run time.

但是在Rust中,我们有 let a = 2; 。在这里,类型(例如Rust中任何整数类型中的 i32 )何时绑定到 a

But in Rust, we have let a = 2;. Here, when does the type (say i32 from any of the integer types in Rust) get bound to a?

我正在构建前端Rust编译器,目前正在编写解析器阶段。此时,我应该为这些变量分配什么类型?

I am building a front-end Rust compiler and am currently writing the parser phase. At this point, what type should I assign to these variables?

推荐答案

类型绑定是在编译时执行的。这是必需的,以便编译器可以发出正确的机器指令(例如, x86_64 处理器不会将两个 i32 以相同的方式将两个 i64 s相乘)。

Type binding is performed at compile time. This is necessary so that the compiler can emit the proper machine instructions (for example, an x86_64 processor doesn't multiply two i32s the same way it multiplies two i64s).

许多动态类型化的语言(例如Python或Lua)将携带类型信息以及值( not 变量)和调度操作在运行时根据每个操作数的类型。另一方面,静态类型的语言(例如C或Rust)通常会丢弃大多数类型信息。没必要,因为执行该操作所需的机器指令直接在可执行文件中发出(这使静态类型的程序比类似的动态类型的程序快)。

Many dynamically-typed languages, such as Python or Lua, will carry type information along with the values (not the variables) and dispatch operations at runtime based on the type of each operand. On the other hand, statically-typed languages, such as C or Rust, usually discard most of the type information; it's not necessary because the machine instructions required to perform the operation were emitted directly in the executable (which makes the statically-typed program faster than the comparable dynamically-typed program).

通过让编译器告诉我们有关类型错误的信息,我们可以证明类型绑定是在编译时完成的(这称为类型检查)。例如:

We can demonstrate that type binding is done at compile time by having the compiler tell us about type errors (this is known as type checking). Here's an example:

fn squared(x: f64) -> f64 {
    x * x
}

fn main() {
    let a = 2i32;
    println!("{}", squared(a));
}

编译后得到以下输出:

error[E0308]: mismatched types
 --> src/main.rs:7:28
  |
7 |     println!("{}", squared(a));
  |                            ^ expected f64, found i32

Rust编译器可以推断基于使用情况的许多变量(类似于 auto 在C ++中的工作方式)。如果无法显示,则会提示错误。例如:

The Rust compiler can infer the type of many variables based on usage (similar to how auto works in C++). When it can't, it'll give an error. For example:

fn main() {
    let a;
}

提供以下输出:

error[E0282]: type annotations needed
 --> src/main.rs:2:9
  |
2 |     let a;
  |         ^
  |         |
  |         cannot infer type for `_`
  |         consider giving `a` a type

当编译器遇到错误时,它会停止并且不会产生可运行的可执行文件。由于我们没有程序的可执行形式,因此没有运行时,因此上述情况发生在编译时。

When the compiler encounters errors, it stops and doesn't produce a runnable executable. Since we don't have an executable form of our program, there is no "run time", so the above happens at compile time.

这篇关于什么时候在Rust中进行类型绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆