Created
November 27, 2021 16:22
-
-
Save cdesch/e83b7f5c8cf7e110c67a1168c1d677e7 to your computer and use it in GitHub Desktop.
Rust Vector
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for account in &accounts { | |
println!("ID: {} Name: {}", account.id, account.name); | |
} | |
// Choose Random Account from Accounts | |
let account = accounts.choose(&mut rand::thread_rng()).unwrap(); | |
println!("{}", account.id); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use rand::thread_rng; | |
use rand::seq::SliceRandom; | |
struct Account { | |
id: u64, | |
name: String, | |
balance: u64, | |
} | |
// Generate Accounts sequentialy | |
fn generate_accounts(num_accounts: u64, balance: u64) -> Vec<Account> { | |
let mut accounts = Vec::new(); | |
for n in 1..num_accounts { | |
// println!("ID: {}",n); | |
let account = Account { | |
id: n, | |
name: format!("Name_{}", n), | |
balance: balance, | |
}; | |
accounts.push(account); | |
} | |
// Return accounts | |
accounts | |
} | |
fn main() { | |
let num_accounts = 10; | |
let num_transactions = 10; | |
let default_balance = 100; | |
// Generate Accounts | |
let accounts = generate_accounts(num_accounts, default_balance); | |
// print accounts | |
for account in &accounts { | |
println!("ID: {} Name: {}", account.id, account.name); | |
} | |
// Choose Random Account from Accounts | |
let account = accounts.choose(&mut rand::thread_rng()).unwrap(); | |
println!("{}", account.id); | |
println!("Finished"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment