Hello World与Cargo

Hello world

新建一个helloworld.rs文件.

fn main(){
    println!("Hello world");
}

rust程序必须先编译后执行。

rustc helloworld.rs
编译可能出现如下错误
C:\Users\wang\Desktop\rust>rustc helloworld.rs
error: linker `link.exe` not found
  |
  = note: program not found

note: the msvc targets depend on the msvc linker but `link.exe` was not found

note: please ensure that VS 2013, VS 2015, VS 2017 or VS 2019 was installed with the Visual C++ option

error: aborting due to previous error

可以采用如下方式修复后重新编译
rustup toolchain install stable-x86_64-pc-windows-gnu
rustup default stable-x86_64-pc-windows-gnu

编译成功后,会在当前目录下生成一个helloworld.exe,helloworld可执行文件(linux)。命令行执行helloworld.exe windows或者./helloworld linux运行程序。

至此helloworld编译运行成功,其中fn main是应用的入口函数,println!是一个宏,表示程序的输出,里面是要输出的内容,程序的语句结束用分号结束。

cargo

Rust的构建工具和包管理工具。

构建工具就是可以帮助我们生成程序的骨架,包管理工具可以帮我们处理包之间的依赖。

新建给一个Demo1的项目,会在当前目录创建一个Demo1的文件夹,源码为src/main.rs
cargo new Demo1

编译
cargo build 

编译release包。生产环境会在提供运行效率,但是编译会相对比较慢,编译之后可以在target/release下找到可执行文件。
运行,如果之前没有编译过,执行此命令会自动编译
cargo run
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容