hello.rs
// hello.rs
fn main(){
println!("Hello, world!");
}
$ rustc hello.rs
$ ./hello
Hello, world!
ソフトウェアエンジニアの技術ブログ:Software engineer tech blog
随机应变 ABCD: Always Be Coding and … : хороший
hello.rs
// hello.rs
fn main(){
println!("Hello, world!");
}
$ rustc hello.rs
$ ./hello
Hello, world!
従来のプログラミング言語の優れた点を採用し、問題点を排除して作成された
// dispfile.rs
use std::env;
use std::io::BufReader;
use std::io::prelude::*;
use std::fs::File;
fn main() -> std::io::Result<()> {
let argv: Vec<String> = env::args().collect(); // コマンドライン引数
if argv.len() < 2 {
println!("引数にファイル名を指定してください。");
std::process:exit(1);
}
let f = File::open(&argv[1])?;
let reader = BufReader::new(f);
for line in reader.lines() {
println!("{}", line?);
}
Ok(())
}
### variables
fn main() {
let x = 13;
println!("{}", x);
let x: f64 = 3.14159;
println!("{}", x);
let x;
x = 0;
println!("{}", x);
}
$ target/debug/hello
13
3.14159
0
variables are modifiable.
mutable: compiler allows read and write
immutable: the compiler will only allow the variable to be read from
mutable values are denoted with a mut keyword.
fn main() {
let mut x = 42;
println!("{}", x);
x = 13;
println!("{}", x);
}
booleans: bool
unsigned integers(符号なし): u8, u16, u32, u64, u128
signed integers(符号あり): i8, i16, i32, i64, i128
pointer sized integers: usize, isize
floating point: f32, f64
tuple: (value, value, …)
arrays:
slices,
str
Numeric types can be explicitly specified by appending the type to the end of the number.(13u32, 2u8)
fn main() {
let x = 12;
let a = 12u8;
let b = 4.3;
let c = 4.3f32;
let bv = true;
let t = (13, false);
let sentence = "hello world";
println!(
"{} {} {} {} {} {} {} {}",
x, a, b, c, bv, t.0, t.1, sentence
)
}
$ target/debug/hello
12 12 4.3 4.3 true 13 false hello world
### Basic Type Conversion
as で変換する
fn main() {
let a = 13u8;
let b = 7u32;
let c = a as u32 + b;
println!("{}", c);
let t = true;
println!("{}", t as u8);
}
$ target/debug/hello
20
1
### constants
constant used many times efficiently.
constant must always have explicit types.
constant names are always in SCREAMING_SNAKE_CASE.
const PI: f32 = 3.14159;
fn main() {
println!(
"to make an apple {} from scratch, you must first create a universe.",
PI
)
}
const PI: u8 = 12; とも書く
### Arrays
an array is a fixed length collection of data.
data type for an array is [T;N] where T is the elements type and N is the fixed length.
Individual elements can be retrieved with the x operator
fn main() {
let nums: [i32; 3] = [1, 2, 3];
println!("{:?}", nums);
println!("{}", nums[1]);
}
$ target/debug/hello
[1, 2, 3]
2
なるほど
$ curl –proto ‘=https’ –tlsv1.2 -sSf https://sh.rustup.rs | sh
$ sudo apt install cargo
$ cargo new –bin hello
$ cd hello
$ cargo build
Compiling hello v0.1.0 (/home/vagrant/dev/rust/hello)
Finished dev [unoptimized + debuginfo] target(s) in 0.32s
$ tree
.
├── Cargo.lock
├── Cargo.toml
├── src
│ └── main.rs
└── target
├── CACHEDIR.TAG
└── debug
├── build
├── deps
│ ├── hello-2ab4b0e0c3958f3e
│ └── hello-2ab4b0e0c3958f3e.d
├── examples
├── hello
├── hello.d
└── incremental
7 directories, 8 files
$ target/debug/hello
Hello, world!
ファイルの中身を見てみる
hello/src/main.rs
fn main() {
println!("Hello, world!");
}
中身をtour of rustと同じにする
fn main() {
println!("Hello, 🦀");
}
$ cargo build
$ target/debug/hello
Hello, 🦀
なるほど…