Skip to content

Instantly share code, notes, and snippets.

@cdesch
Created November 27, 2021 16:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cdesch/e83b7f5c8cf7e110c67a1168c1d677e7 to your computer and use it in GitHub Desktop.
Save cdesch/e83b7f5c8cf7e110c67a1168c1d677e7 to your computer and use it in GitHub Desktop.
Rust Vector
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);
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