### 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
なるほど