Rust 专题
您的位置:rust > Rust专题 > Rust第一个程序
Rust第一个程序
作者:--    发布时间:2019-11-20

在本文中,使用rust语言编写简单的程序,以了解如何编写,保存和编译rust程序。现在,打开记事本文件并编写以下代码:

fn main(){
    println!("hello, world!");
}

将上面内容保存到一个文件:hello.rs 中,然后使用 rustc hello.rs 命令来编译运行上面程序,得到以下结果 -

hello, world!
  • main()main()函数始终是每个rust可执行代码中的第一个代码。 main()函数用大括号{}括起来。 main()函数不包含任何参数,也不返回任何值。
  • println!:这是一个rust宏。 如果它调用该函数,则它不包含符号:'!'
  • "hello world":它是作为参数传递给println!的字符串,字符串将打印到控制台。

创建,编译和运行程序的过程

  1. 打开记事本文件并将代码写入记事本文件中。
  2. 使用.rs扩展名保存文件。
  3. 打开命令提示符
  4. 设置目录的路径,假设项目位于/home/hema/worsp/rust目录中。
  5. 使用rustc命令编译上述程序。
  6. 最后,使用命令./filename运行程序。
hema@h3:~/worsp/rust$ rustc hello.rs && ./hello
hello world!

注:如果有遇到 “error: could not exec the linker link.exe: “ 之类的错误,请下载 build tools for visual studio 2017 (url => https://visualstudio.microsoft.com/downloads/#title-58852 )


网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册