Â
Rustã®ãªãã©ã«ã¨æ°å¤ã®è¡¨è¨ã«ã¤ãã¦
Rustã¯ããã®è¨èªã®åºç¤çãªè¦ç´ ã®1ã¤ã¨ãã¦ããªãã©ã«ãæ°å¤è¡¨è¨ãæä¾ãã¦ãã¾ããæ¬è¨äºã§ã¯ãRustã«ãããåºæ¬çãªãªãã©ã«ã®ä½¿ãæ¹ãå½¢å¼ãæ°å¤è¡¨è¨ã®ç¨®é¡ã«ã¤ãã¦è©³ãã解説ãã¾ãã
ãªãã©ã«ã¨ã¯ä½ã
ãªãã©ã«ã¨ã¯ãRustã®ããã°ã©ã å ã§å¤ãç´æ¥è¡¨ç¤ºãããã®ã§ããä¾ãã°ãæ°å¤ãæååããã¼ã«å¤ãªã©ãå«ã¾ãã¾ãããªãã©ã«ã¯ãããããã®ãã¼ã¿åã«æ±ºãããã¦ãã¾ãã
主ãªãªãã©ã«ã®ç¨®é¡ï¼
- ã¹ã«ã©ã¼å: æ°å¤ããã¼ã«å¤ãæåãæµ®åå°æ°ç¹å¤ãªã©
- ã³ã¬ã¯ã·ã§ã³å: ãªã¹ããé åãã¿ãã«ãªã©
Rustã®ãªãã©ã«ã¯ã使ããããã¨å®è¡æ§ãèæ ®ããè¨è¨ã«ãªã£ã¦ãã¾ãã
æ°å¤ã®è¡¨è¨ã®ç¨®é¡
Rustã§ã¯ãè¤æ°ã®æ°å¤è¡¨è¨ãæä¾ããã¦ãããããããã«ãã¼ã¿åãæå®ããã¾ãã主ãªæ°å¤ã®è¡¨è¨ã¯ä»¥ä¸ã®éãã§ãã
æ£æ°ã¨è² æ°
42
(æ£æ°)-42
(è² æ°)
åå é²æ°è¡¨è¨
0x2A
(åå é²æ°ã®42)
å «é²æ°è¡¨è¨
0o52
(å «é²æ°ã®42)
äºé²æ°è¡¨è¨
0b101010
(äºé²æ°ã®42)
å°æ°ç¹ãå«ãå¤
3.14
(æµ®åå°æ°ç¹æ°)
åæå®å®æ°
Rustã§ã¯ãå¤ã®åãæå®ããããã«ãµãã£ãã¯ã¹ã使ç¨ãã¾ãããã¼ã¿åãæ示çã«æå®ãããã¨ãã§ãã¾ãã
let x: i32 = 42;
let y: f64 = 3.14;
åãµãã£ãã¯ã¹ä¸è¦§
åå | 説æ |
---|---|
i8 , u8 |
8ãããæ´æ°å |
i16 , u16 |
16ãããæ´æ°å |
i32 , u32 |
32ãããæ´æ°å |
i64 , u64 |
64ãããæ´æ°å |
i128 , u128 |
128ãããæ´æ°å |
f32 |
32ãããæµ®åå°æ°ç¹å |
f64 |
64ãããæµ®åå°æ°ç¹å |
å®ç¨çãªã¨ãã»ã³ã¹ã¨ãã¯ããã¯
Rustã§é·ãæ°å¤ãè¦ãããããï¼ã¢ã³ãã¼ã¹ã³ã¢ã®æ´»ç¨
Rustã§ã¯ãé·ãæ°å¤ã«ã¢ã³ãã¼ã¹ã³ã¢ï¼_ï¼ãæ¿å ¥ãããã¨ã§ãæ¡æ°ãå¤ãè¦èªæ§ãä½ãæ°å¤ãããèªã¿ããããããã¨ãã§ãã¾ãã// 符å·ãªã8ãããæ´æ° (0ãã255ã¾ã§ã®æ´æ°)
let u8_number = 255_u8;
// 符å·ä»ã32ãããæ´æ° (-2,147,483,648ãã2,147,483,647ã¾ã§ã®æ´æ°)
let i32_number = -2147_483_648_i32;
// 符å·ãªã64ãããæ´æ°
let u64_number = 18_446_744_073_709_551_615_u64;
// f32åæµ®åå°æ°ç¹æ° (å精度浮åå°æ°ç¹æ°)
let f32_number = 3.14159_265_f32;
// f64åæµ®åå°æ°ç¹æ° (å精度浮åå°æ°ç¹æ°)
let f64_number = 2.71828_18284_59045_f64;
// ç§å¦è¡¨è¨æ³
let scientific_notation = 1.2345e10; // 12345000000
// ãã¤ããªãªãã©ã«
let binary_number = 0b1111_0000_1111_0000;
// ãªã¯ã¿ã«ãªãã©ã«
let octal_number = 0o777;
ããã©ã«ãå
ãªãã©ã«ã®ããã©ã«ãåï¼
- æ´æ°ãªãã©ã«:
i32
- æµ®åå°æ°ç¹ãªãã©ã«:
f64
åæ¨è«ãåããªãå ´åãæ示çã«åãæå®ããå¿ è¦ãããã¾ãã
let x = 42; // ããã©ã«ãã§i32
let y = 3.14; // ããã©ã«ãã§f64
let z: i64 = 100; // æ示çã«i64ãæå®
åã®å¤æ
as
ãã¼ã¯ã¼ãã使ç¨ãã¦åãå¤æãã¾ããå¤ã®ç¯å²ã精度ã«æ³¨æãå¿
è¦ã§ãã
let x: i32 = 42;
let y: f64 = x as f64 + 0.5; // i32ããf64ã«å¤æ
let z: u8 = x as u8; // i32ããu8ã«å¤æ
æµ®åå°æ°ç¹ãªãã©ã«ã®ææ°è¡¨è¨
ææ°è¡¨è¨ã使ç¨ãã¦ãé常ã«å¤§ããªå¤ãå°ããªå¤ã表ç¾ã§ãã¾ãã
let large = 1.2e10; // 1.2 x 10^10
let small = 3.4e-5; // 3.4 x 10^-5
Rustã®å¼ã®åªå é ä½
Rustã§ã¯ãå¼ã®åªå é ä½ãå³å¯ã«å®ç¾©ããã¦ãã¾ãã以ä¸ã®è¡¨ã¯ãæ¼ç®åã¨ãã®åªå é ä½ã示ãã¦ãã¾ãã
åªå é ä½ | ã«ãã´ãª | ä¾ |
---|---|---|
1 (æé«) | æ¬å¼§ | (expression) |
2 | ã¡ã½ããå¼ã³åºã | x.method() , x[0] |
3 | ä¸å æ¼ç®å | -x , !x |
4 | ä¹ç®ã»é¤ç®ã»å°ä½æ¼ç® | * , / , % |
5 | å ç®ã»æ¸ç® | + , - |
6 | ãããã·ãã | << , >> |
7 | æ¯è¼æ¼ç®å | < , > , <= , >= |
8 | ç価æ¼ç®å | == , != |
9 | ãããAND | & |
10 | ãããXOR | ^ |
11 | ãããOR | | |
12 | è«çAND | && |
13 | è«çOR | || |
14 | ä»£å ¥æ¼ç®å | = , += , -= , *= |
ãã®åªå é ä½ã«åºã¥ãã¦ãRustã³ã³ãã¤ã©ã¯è¤éãªå¼ã®è§£éé åºã決å®ãã¾ããå¿ è¦ã«å¿ãã¦æ¬å¼§ã使ç¨ãããã¨ã§ãæå³ããè¨ç®é åºãå¼·å¶ã§ãã¾ãã
let x = 2 + 3 * 4; // xã¯14ã«ãªã (3 * 4ãå
ã«è¨ç®ããã)
let y = (2 + 3) * 4; // yã¯20ã«ãªã (2 + 3ãå
ã«è¨ç®ããã)
åèãªã³ã¯