在本文中,使用rust语言编写简单的程序,以了解如何编写,保存和编译rust程序。现在,打开记事本文件并编写以下代码:
fn main(){
println!("hello, world!");
}
将上面内容保存到一个文件:hello.rs 中,然后使用 rustc hello.rs 命令来编译运行上面程序,得到以下结果 -
hello, world!
main():main()函数始终是每个rust可执行代码中的第一个代码。 main()函数用大括号{}括起来。 main()函数不包含任何参数,也不返回任何值。println!:这是一个rust宏。 如果它调用该函数,则它不包含符号:'!'。"hello world":它是作为参数传递给println!的字符串,字符串将打印到控制台。.rs扩展名保存文件。/home/hema/worsp/rust目录中。rustc命令编译上述程序。./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 )