Rust 程序设计语言 简体中文版 1.85.0为这是一个表达式,我们想要返回它的值。 让我们看看另一个例子: 文件名:src/main.rs fn main() { let x = plus_one(5); println!("The value of x is: {x}"); } fn plus_one(x: i32) -> i32 { x + 1 } 运行代码会打印出 The value of x is: 6。但如果在包含 表达式变成语句,我们将看到一个错误。 文件名:src/main.rs fn main() { let x = plus_one(5); 55/562Rust 程序设计语言 简体中文版 println!("The value of x is: {x}"); } fn plus_one(x: i32) -> i32 { x + 1; } 运行代码会产生一个错误,如下: $ cargo v0.1.0 (file:///projects/functions) error[E0308]: mismatched types --> src/main.rs:7:24 | 7 | fn plus_one(x: i32) -> i32 { | -------- ^^^ expected `i32`, found `()` | | | implicitly0 码力 | 562 页 | 3.23 MB | 29 天前3
共 1 条
- 1













