mirror of
https://github.com/ahgamut/rust-ape-example.git
synced 2024-05-15 10:02:42 +00:00
4b8d56098f
they don't run yet because of some stack size thing
64 lines
1.3 KiB
Rust
64 lines
1.3 KiB
Rust
// ./src/std/hash/alt_key_types.md
|
|
|
|
use std::collections::HashMap;
|
|
|
|
// Eq requires that you derive PartialEq on the type.
|
|
#[derive(PartialEq, Eq, Hash)]
|
|
struct Account<'a>{
|
|
username: &'a str,
|
|
password: &'a str,
|
|
}
|
|
|
|
struct AccountInfo<'a>{
|
|
name: &'a str,
|
|
email: &'a str,
|
|
}
|
|
|
|
type Accounts<'a> = HashMap<Account<'a>, AccountInfo<'a>>;
|
|
|
|
fn try_logon<'a>(accounts: &Accounts<'a>,
|
|
username: &'a str, password: &'a str){
|
|
println!("Username: {}", username);
|
|
println!("Password: {}", password);
|
|
println!("Attempting logon...");
|
|
|
|
let logon = Account {
|
|
username,
|
|
password,
|
|
};
|
|
|
|
match accounts.get(&logon) {
|
|
Some(account_info) => {
|
|
println!("Successful logon!");
|
|
println!("Name: {}", account_info.name);
|
|
println!("Email: {}", account_info.email);
|
|
},
|
|
_ => println!("Login failed!"),
|
|
}
|
|
}
|
|
|
|
fn part0(){
|
|
let mut accounts: Accounts = HashMap::new();
|
|
|
|
let account = Account {
|
|
username: "j.everyman",
|
|
password: "password123",
|
|
};
|
|
|
|
let account_info = AccountInfo {
|
|
name: "John Everyman",
|
|
email: "j.everyman@email.com",
|
|
};
|
|
|
|
accounts.insert(account, account_info);
|
|
|
|
try_logon(&accounts, "j.everyman", "psasword123");
|
|
|
|
try_logon(&accounts, "j.everyman", "password123");
|
|
}
|
|
|
|
pub fn main() {
|
|
part0();
|
|
}
|
|
|