2.2.Hello world!

Source: Internet
Author: User

Hello world!

Now that you've installed rust, let's start writing the first rust program. Printing Hello world has become a tradition of learning a new language. The benefit of running a simple program is to prove that your compiler is not just installed on your computer, but that it works perfectly. and printing characters is a normal thing.

The first thing we need to do is create a file and then knock the code in. I'm used to creating a new project folder under My Documents directory and putting all my code there. Rust doesn't care where your code is.

The real thing we need to be concerned with is this tutorial will assume you have a command-line base. Rust doesn't require you to use any editing tools, or where you put the code. If you prefer the IDE, maybe you want to see Solidoak, or other plugins that you can use for your favorite IDE. There are many communities that maintain a wide variety of extensions. The rust team is also maintaining some of the editor's plugins. Because the configuration editor or IDE is beyond the scope of this tutorial, see the other tutorials separately.

Gossip Less, we first create a new folder into the projects.

$ mkdir ~/projects
$ CD ~/projects
$ mkdir Hello_world
$ CD Hello_world

If you are on the Windows platform and do not use PowerShell, there may be an error. Check out other documentation to learn how to use your shell.

Next, let's create a new source file. We named it main.rs. Rust files always have a. rs extension. If you use more than two words as the file name, use the underscore: hello_world.rs instead of helloworld.rs.

Now please open your file and enter:
fn Main () {
println! ("hello,world!");
}

Save the file, and then enter the following in the terminal:

$ RUSTC main.rs
$./main # or Main.exe on Windows
Hello, world!.

It worked! Let's explore the details.

fn Main () {

}

They define a main function, and the main function is a bit special: it's the beginning of any rust program. The first line of code means: "Hey, I've declared a function called Main, which has no parameters and no return value." "If parameters are needed, they will appear in parentheses." Because we don't return anything in the function, we can omit the return type. We'll know that in a minute.

You also notice that the function is surrounded by a pair of curly braces, and rust needs curly braces to wrap the body of the function. You can also use a good style to declare a function: Put curly braces on the same line, with a space between the brackets.

This is the next line:

println! ("Hello, world!");

This line does all the work of this little program. There are many important details in this. The first is that it uses four spaces to indent instead of tab. Please configure your editor to change the TAB key to four spaces. We offer a number of similar configurations to many editor plugins.

2nd, println!. () section. It called a Rust macro, which was done by metaprogramming [original: Which is what metaprogramming is doing in Rust.]. If it is a function, it will look like this: println (). We don't need to know the difference between them at the moment, just know that sometimes you see an exclamation point and sometimes not. For a good reason, rust runs println! as a macro when it executes. But this is a high-level topic. Last but not least: Rust's macros are distinctly different from the C language, and if you use macros, don't be afraid of them. We will eventually be aware of these details. You have to trust us now.

In addition, "hello,world!" is a ' string '. String is an extremely complex topic, which is a statically assigned string. If you want to learn about the allocation mechanism, read the documentation on heaps and stacks. We provide a string as a parameter to println!, so that it prints the string to the screen. True TM is simple.

Finally, each command is used with one; Rust is an expression-oriented language, * meaning that its body is mostly an expression, not a statement [original: Which means that most things is expressions, rather than statements.] A semicolon, usually representing the end of an order, and the next sentence is about to start executing. Most of the rust code used;

Also, compile and run our program. We can use the compiler--RUSTC to compile our program, using the following command:

$ RUSTC main.rs

This command is similar to GCC or clang. Rust will output the binary executable file:

$ ls
Main main.rs

or under Windows:

$ dir
Main.exe main.rs

Now we have two files: one is the source code and the other is the executable file.

$./main # or Main.exe on Windows

Enter this command to print out the hello,world! on the terminal

If you have previously studied dynamic languages such as Ruby,python or JavaScript, you may not have performed the above steps. Rust is a "compile-and-run" language, meaning that you can compile the source code into a program, and then it can run in other places where rust is not installed. If you give someone a. rb. py or. js file, he must have Ruby/python/javascript installed to run the files, and you only need a single command to run and compile your files. Everything needs a compromise, and rust has made its choice.

Congratulations, you have finished your first rust program and you have become a rust programmer! Welcome!

Next, I'd like to introduce you to another tool, Cargo, which is often used to write real rust projects, RUSTC is just for small programs, and as your project grows larger, RUSTC becomes weak, so you need a powerful tool. Cargo can also make your code easier to share.

2.2.Hello world!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.