Hello World in Rust
Created
Modified
Build and Run
In our main.rs
, add the following code:
// main.rs
fn main() {
println!("Hello,世界,こんにちは,안녕하세요")
}
Hello,世界,こんにちは,안녕하세요
we can run our application by typing:
Build
rustc main.rs
./main
Hello,世界,こんにちは,안녕하세요
Using cargo Command
Generating a new project
To start, we’ll use Cargo to make a new project for us. For example,
cargo
cargo new hello-rust
Created binary (application) `hello-rust` package
This will generate a new directory called hello-rust with the following files:
hello-rust
|- Cargo.toml #is the manifest file for Rust.
|- src
|- main.rs #is where we’ll write our application code.
Run this program
We can run this program by moving into the new directory that we made and running this in our terminal:
cargo
cd hello-rust
cargo run
Compiling hello-rust v0.1.0 (/home/rust/hello-rust) Finished dev [unoptimized + debuginfo] target(s) in 2.68s Running `target/debug/hello-rust` Hello, world!
Hello fellow Rustaceans!
A small Rust application. For example,
use ferris_says::say; // from the previous step
use std::io::{stdout, BufWriter};
fn main() {
let stdout = stdout();
let message = String::from("Hello fellow Rustaceans!");
let width = message.chars().count();
let mut writer = BufWriter::new(stdout.lock());
say(message.as_bytes(), width, &mut writer).unwrap();
}
Run this program:
cargo
cargo run
__________________________ < Hello fellow Rustaceans! > -------------------------- \ \ _~^~^~_ \) / o o \ (/ '_ - _' / '-----' \