First day of rust study

Local Documentation

The installation of Rust also includes a copy of the documentation locally, so you can read it offline. Run rustup doc to open the local documentation in your browser.

Any time a type or function is provided by the standard library and you’re not sure what it does or how to use it, use the application programming interface (API) documentation to find out!

Writing and Running a Rust Program

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

On Windows, enter the command .\main.exe instead of ./main:

Anatomy of a Rust Program


#![allow(unused)]
fn main() {
    println!("Hello, world!");
}

This line does all the work in this little program: it prints text to the screen. There are four important details to notice here.

  • First, Rust style is to indent with four spaces, not a tab.

  • Second, println! calls a Rust macro. If it called a function instead, it would be entered as println (without the !). We’ll discuss Rust macros in more detail in Chapter 19. For now, you just need to know that using a ! means that you’re calling a macro instead of a normal function, and that macros don’t always follow the same rules as functions.

  • Third, you see the "Hello, world!" string. We pass this string as an argument to println!, and the string is printed to the screen.

  • Fourth, we end the line with a semicolon (;), which indicates that this expression is over and the next one is ready to begin. Most lines of Rust code end with a semicolon.

Compiling and Running Are Separate Steps

You’ve just run a newly created program, so let’s examine each step in the process.

Before running a Rust program, you must compile it using the Rust compiler by entering the rustc command and passing it the name of your source file, like this:

$ rustc main.rs

全部评论

相关推荐

12-27 22:21
门头沟学院 Java
点赞 评论 收藏
分享
稽鱼:简历好丑啊,换个模板,别用红色字体
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务