■ rand 크레이트를 사용해 난수를 생성하는 방법을 보여준다.
▶ Cargo.toml
1 2 3 4 5 6 7 8 9 10 11 |
[package] name = "test_project" version = "0.1.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] rand = "0.8.0" |
▶ main.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
use rand::Rng; fn main() { let mut random = rand::thread_rng(); for _ in 0..5 { let random_value = random.gen_range(1..=6); println!("{}", random_value); } } |