看了 此争论,想体验一下 Rust。

在官网首页的 马上开始 -> 入门 里,第一项 在线试用

打开发现,默认代码居然是中文命名变量这么智能么(后发现应该是老早啥时候自己试改过)。

fn main() {
    let 我的名字 = "Manish";
    
}

编译后报警,确认了一下并非中文命名的原因:

Compiling playground v0.0.1 (/playground)
warning: unused variable: `我的名字`
 --> src/main.rs:2:9
  |
2 |     let 我的名字 = "Manish";
  |         ^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_我的名字`
  |
  = note: `#[warn(unused_variables)]` on by default

warning: `playground` (bin "playground") generated 1 warning

在线环境里看到各种配套工具和设置,但没找到例程和教程。挑了首页上的 “Learn -> Rust By Example”。

第一个 Hello World 例子,加了句:

println!(我的名字)

结果报错:

Exited with status 101

Standard Error

   Compiling playground v0.0.1 (/playground)
error: format argument must be a string literal
 --> src/main.rs:3:14
  |
3 |     println!(我的名字)
  |              ^^^^^^^^
  |
help: you might be missing a string literal to format with
  |
3 |     println!("{}", 我的名字)
  |              +++++

error: could not compile `playground` (bin "playground") due to 1 previous error

这个虽然能理解,不过如果从 Python 过来需要习惯一下。

fn main() {
    let 我的名字 = "无名";
    println!("{}", 我的名字)
}

成功输出:

Standard Output

无名

按照 练习要求,最后加了一句:

    println!("I'm a Rustacean!")

编译报错:

   Compiling playground v0.0.1 (/playground)
error: expected `;`, found `println`
 --> src/main.rs:3:25
  |
3 |     println!("{}", 我的名字)
  |                             ^ help: add `;` here
4 |     println!("I'm a Rustacean!")
  |     ------- unexpected token

这里就有点迷惑,大概是在线环境的问题?之前 println!("{}", 我的名字) 末尾没分号,但运行无误。

只加了一个分号,末尾仍然没加:

fn main() {
    let 我的名字 = "无名";
    println!("{}", 我的名字);
     println!("I'm a Rustacean!")
}

果然仍运行无误。

初步感觉,新手常碰到的坑基本在报错信息中有足够提示。希望早出中文版本的报警等反馈信息。

【待续】