Skip to main content

ExchangeRateAPI

Struct ExchangeRateAPI 

Source
pub struct ExchangeRateAPI { /* private fields */ }
Expand description

Client for the Exchange Rate API.

Create an instance with ExchangeRateAPI::new and call methods to interact with the API endpoints.

§Example

use exchange_rateapi::ExchangeRateAPI;

let client = ExchangeRateAPI::new("era_live_your_api_key");
let rates = client.latest("USD", None).unwrap();
println!("{:?}", rates.rates);

Implementations§

Source§

impl ExchangeRateAPI

Source

pub fn new(api_key: &str) -> Self

Creates a new ExchangeRateAPI client.

§Arguments
Examples found in repository?
examples/basic.rs (line 16)
11fn main() {
12    // Read API key from environment
13    let api_key = std::env::var("EXCHANGE_RATE_API_KEY")
14        .expect("Set EXCHANGE_RATE_API_KEY environment variable");
15
16    let client = ExchangeRateAPI::new(&api_key);
17
18    // -----------------------------------------------------------------------
19    // 1. Get latest rates
20    // -----------------------------------------------------------------------
21    println!("--- Latest Rates (USD) ---");
22    match client.latest("USD", Some("EUR,GBP,JPY,CAD")) {
23        Ok(resp) => {
24            for (currency, rate) in &resp.rates {
25                println!("  {} = {}", currency, rate);
26            }
27        }
28        Err(e) => eprintln!("Error: {}", e),
29    }
30
31    // -----------------------------------------------------------------------
32    // 2. Convert currency
33    // -----------------------------------------------------------------------
34    println!("\n--- Convert 100 USD to EUR ---");
35    match client.convert("USD", "EUR", 100.0) {
36        Ok(resp) => {
37            println!("  {} {} = {} {}", resp.amount, resp.from, resp.result, resp.to);
38            println!("  Rate: {}", resp.rate);
39        }
40        Err(e) => eprintln!("Error: {}", e),
41    }
42
43    // -----------------------------------------------------------------------
44    // 3. Get a single exchange rate
45    // -----------------------------------------------------------------------
46    println!("\n--- Single Rate: GBP/JPY ---");
47    match client.get_rate("GBP", "JPY") {
48        Ok(rate) => println!("  1 GBP = {} JPY", rate),
49        Err(e) => eprintln!("Error: {}", e),
50    }
51
52    // -----------------------------------------------------------------------
53    // 4. Historical rates for a specific date
54    // -----------------------------------------------------------------------
55    println!("\n--- Historical Rates (2025-01-15, EUR base) ---");
56    match client.for_date("2025-01-15", "EUR", Some("USD,GBP")) {
57        Ok(resp) => {
58            println!("  Date: {}", resp.date);
59            for (currency, rate) in &resp.rates {
60                println!("  {} = {}", currency, rate);
61            }
62        }
63        Err(e) => eprintln!("Error: {}", e),
64    }
65
66    // -----------------------------------------------------------------------
67    // 5. Time series
68    // -----------------------------------------------------------------------
69    println!("\n--- Time Series (USD/EUR, Jan 2025) ---");
70    match client.time_series("2025-01-01", "2025-01-07", "USD", Some("EUR")) {
71        Ok(resp) => {
72            let mut dates: Vec<&String> = resp.rates.keys().collect();
73            dates.sort();
74            for date in dates {
75                if let Some(rate) = resp.rates[date].get("EUR") {
76                    println!("  {}: EUR = {}", date, rate);
77                }
78            }
79        }
80        Err(e) => eprintln!("Error: {}", e),
81    }
82
83    // -----------------------------------------------------------------------
84    // 6. List all symbols
85    // -----------------------------------------------------------------------
86    println!("\n--- Supported Currencies (first 10) ---");
87    match client.symbols() {
88        Ok(resp) => {
89            let mut symbols: Vec<(&String, &String)> = resp.symbols.iter().collect();
90            symbols.sort_by_key(|(code, _)| code.to_owned());
91            for (code, name) in symbols.iter().take(10) {
92                println!("  {}: {}", code, name);
93            }
94            println!("  ... and {} more", resp.symbols.len().saturating_sub(10));
95        }
96        Err(e) => eprintln!("Error: {}", e),
97    }
98
99    // -----------------------------------------------------------------------
100    // 7. Historical rates with preset period
101    // -----------------------------------------------------------------------
102    println!("\n--- Last 7 Days: USD/EUR ---");
103    match client.get_historical_rates("USD", "EUR", Period::SevenDays) {
104        Ok(resp) => {
105            let mut dates: Vec<&String> = resp.rates.keys().collect();
106            dates.sort();
107            for date in dates {
108                if let Some(rate) = resp.rates[date].get("EUR") {
109                    println!("  {}: {}", date, rate);
110                }
111            }
112        }
113        Err(e) => eprintln!("Error: {}", e),
114    }
115}
Source

pub fn latest( &self, base: &str, symbols: Option<&str>, ) -> Result<LatestResponse>

Fetches the latest exchange rates for a base currency.

§Arguments
  • base - The base currency code (e.g. "USD").
  • symbols - Optional comma-separated list of target currencies (e.g. Some("EUR,GBP,JPY")). Pass None to get all available currencies.
§Example
let client = ExchangeRateAPI::new("era_live_xxx");
let resp = client.latest("USD", Some("EUR,GBP")).unwrap();
println!("EUR rate: {}", resp.rates["EUR"]);
Examples found in repository?
examples/basic.rs (line 22)
11fn main() {
12    // Read API key from environment
13    let api_key = std::env::var("EXCHANGE_RATE_API_KEY")
14        .expect("Set EXCHANGE_RATE_API_KEY environment variable");
15
16    let client = ExchangeRateAPI::new(&api_key);
17
18    // -----------------------------------------------------------------------
19    // 1. Get latest rates
20    // -----------------------------------------------------------------------
21    println!("--- Latest Rates (USD) ---");
22    match client.latest("USD", Some("EUR,GBP,JPY,CAD")) {
23        Ok(resp) => {
24            for (currency, rate) in &resp.rates {
25                println!("  {} = {}", currency, rate);
26            }
27        }
28        Err(e) => eprintln!("Error: {}", e),
29    }
30
31    // -----------------------------------------------------------------------
32    // 2. Convert currency
33    // -----------------------------------------------------------------------
34    println!("\n--- Convert 100 USD to EUR ---");
35    match client.convert("USD", "EUR", 100.0) {
36        Ok(resp) => {
37            println!("  {} {} = {} {}", resp.amount, resp.from, resp.result, resp.to);
38            println!("  Rate: {}", resp.rate);
39        }
40        Err(e) => eprintln!("Error: {}", e),
41    }
42
43    // -----------------------------------------------------------------------
44    // 3. Get a single exchange rate
45    // -----------------------------------------------------------------------
46    println!("\n--- Single Rate: GBP/JPY ---");
47    match client.get_rate("GBP", "JPY") {
48        Ok(rate) => println!("  1 GBP = {} JPY", rate),
49        Err(e) => eprintln!("Error: {}", e),
50    }
51
52    // -----------------------------------------------------------------------
53    // 4. Historical rates for a specific date
54    // -----------------------------------------------------------------------
55    println!("\n--- Historical Rates (2025-01-15, EUR base) ---");
56    match client.for_date("2025-01-15", "EUR", Some("USD,GBP")) {
57        Ok(resp) => {
58            println!("  Date: {}", resp.date);
59            for (currency, rate) in &resp.rates {
60                println!("  {} = {}", currency, rate);
61            }
62        }
63        Err(e) => eprintln!("Error: {}", e),
64    }
65
66    // -----------------------------------------------------------------------
67    // 5. Time series
68    // -----------------------------------------------------------------------
69    println!("\n--- Time Series (USD/EUR, Jan 2025) ---");
70    match client.time_series("2025-01-01", "2025-01-07", "USD", Some("EUR")) {
71        Ok(resp) => {
72            let mut dates: Vec<&String> = resp.rates.keys().collect();
73            dates.sort();
74            for date in dates {
75                if let Some(rate) = resp.rates[date].get("EUR") {
76                    println!("  {}: EUR = {}", date, rate);
77                }
78            }
79        }
80        Err(e) => eprintln!("Error: {}", e),
81    }
82
83    // -----------------------------------------------------------------------
84    // 6. List all symbols
85    // -----------------------------------------------------------------------
86    println!("\n--- Supported Currencies (first 10) ---");
87    match client.symbols() {
88        Ok(resp) => {
89            let mut symbols: Vec<(&String, &String)> = resp.symbols.iter().collect();
90            symbols.sort_by_key(|(code, _)| code.to_owned());
91            for (code, name) in symbols.iter().take(10) {
92                println!("  {}: {}", code, name);
93            }
94            println!("  ... and {} more", resp.symbols.len().saturating_sub(10));
95        }
96        Err(e) => eprintln!("Error: {}", e),
97    }
98
99    // -----------------------------------------------------------------------
100    // 7. Historical rates with preset period
101    // -----------------------------------------------------------------------
102    println!("\n--- Last 7 Days: USD/EUR ---");
103    match client.get_historical_rates("USD", "EUR", Period::SevenDays) {
104        Ok(resp) => {
105            let mut dates: Vec<&String> = resp.rates.keys().collect();
106            dates.sort();
107            for date in dates {
108                if let Some(rate) = resp.rates[date].get("EUR") {
109                    println!("  {}: {}", date, rate);
110                }
111            }
112        }
113        Err(e) => eprintln!("Error: {}", e),
114    }
115}
Source

pub fn convert( &self, from: &str, to: &str, amount: f64, ) -> Result<ConvertResponse>

Converts an amount from one currency to another.

§Arguments
  • from - Source currency code (e.g. "USD").
  • to - Target currency code (e.g. "EUR").
  • amount - The amount to convert.
§Example
let client = ExchangeRateAPI::new("era_live_xxx");
let resp = client.convert("USD", "EUR", 250.0).unwrap();
println!("250 USD = {} EUR", resp.result);
Examples found in repository?
examples/basic.rs (line 35)
11fn main() {
12    // Read API key from environment
13    let api_key = std::env::var("EXCHANGE_RATE_API_KEY")
14        .expect("Set EXCHANGE_RATE_API_KEY environment variable");
15
16    let client = ExchangeRateAPI::new(&api_key);
17
18    // -----------------------------------------------------------------------
19    // 1. Get latest rates
20    // -----------------------------------------------------------------------
21    println!("--- Latest Rates (USD) ---");
22    match client.latest("USD", Some("EUR,GBP,JPY,CAD")) {
23        Ok(resp) => {
24            for (currency, rate) in &resp.rates {
25                println!("  {} = {}", currency, rate);
26            }
27        }
28        Err(e) => eprintln!("Error: {}", e),
29    }
30
31    // -----------------------------------------------------------------------
32    // 2. Convert currency
33    // -----------------------------------------------------------------------
34    println!("\n--- Convert 100 USD to EUR ---");
35    match client.convert("USD", "EUR", 100.0) {
36        Ok(resp) => {
37            println!("  {} {} = {} {}", resp.amount, resp.from, resp.result, resp.to);
38            println!("  Rate: {}", resp.rate);
39        }
40        Err(e) => eprintln!("Error: {}", e),
41    }
42
43    // -----------------------------------------------------------------------
44    // 3. Get a single exchange rate
45    // -----------------------------------------------------------------------
46    println!("\n--- Single Rate: GBP/JPY ---");
47    match client.get_rate("GBP", "JPY") {
48        Ok(rate) => println!("  1 GBP = {} JPY", rate),
49        Err(e) => eprintln!("Error: {}", e),
50    }
51
52    // -----------------------------------------------------------------------
53    // 4. Historical rates for a specific date
54    // -----------------------------------------------------------------------
55    println!("\n--- Historical Rates (2025-01-15, EUR base) ---");
56    match client.for_date("2025-01-15", "EUR", Some("USD,GBP")) {
57        Ok(resp) => {
58            println!("  Date: {}", resp.date);
59            for (currency, rate) in &resp.rates {
60                println!("  {} = {}", currency, rate);
61            }
62        }
63        Err(e) => eprintln!("Error: {}", e),
64    }
65
66    // -----------------------------------------------------------------------
67    // 5. Time series
68    // -----------------------------------------------------------------------
69    println!("\n--- Time Series (USD/EUR, Jan 2025) ---");
70    match client.time_series("2025-01-01", "2025-01-07", "USD", Some("EUR")) {
71        Ok(resp) => {
72            let mut dates: Vec<&String> = resp.rates.keys().collect();
73            dates.sort();
74            for date in dates {
75                if let Some(rate) = resp.rates[date].get("EUR") {
76                    println!("  {}: EUR = {}", date, rate);
77                }
78            }
79        }
80        Err(e) => eprintln!("Error: {}", e),
81    }
82
83    // -----------------------------------------------------------------------
84    // 6. List all symbols
85    // -----------------------------------------------------------------------
86    println!("\n--- Supported Currencies (first 10) ---");
87    match client.symbols() {
88        Ok(resp) => {
89            let mut symbols: Vec<(&String, &String)> = resp.symbols.iter().collect();
90            symbols.sort_by_key(|(code, _)| code.to_owned());
91            for (code, name) in symbols.iter().take(10) {
92                println!("  {}: {}", code, name);
93            }
94            println!("  ... and {} more", resp.symbols.len().saturating_sub(10));
95        }
96        Err(e) => eprintln!("Error: {}", e),
97    }
98
99    // -----------------------------------------------------------------------
100    // 7. Historical rates with preset period
101    // -----------------------------------------------------------------------
102    println!("\n--- Last 7 Days: USD/EUR ---");
103    match client.get_historical_rates("USD", "EUR", Period::SevenDays) {
104        Ok(resp) => {
105            let mut dates: Vec<&String> = resp.rates.keys().collect();
106            dates.sort();
107            for date in dates {
108                if let Some(rate) = resp.rates[date].get("EUR") {
109                    println!("  {}: {}", date, rate);
110                }
111            }
112        }
113        Err(e) => eprintln!("Error: {}", e),
114    }
115}
Source

pub fn for_date( &self, date: &str, base: &str, symbols: Option<&str>, ) -> Result<HistoricalResponse>

Fetches historical exchange rates for a specific date.

§Arguments
  • date - The date in YYYY-MM-DD format.
  • base - The base currency code (e.g. "USD").
  • symbols - Optional comma-separated list of target currencies.
§Example
let client = ExchangeRateAPI::new("era_live_xxx");
let resp = client.for_date("2025-01-15", "USD", Some("EUR,GBP")).unwrap();
println!("Historical EUR rate: {}", resp.rates["EUR"]);
Examples found in repository?
examples/basic.rs (line 56)
11fn main() {
12    // Read API key from environment
13    let api_key = std::env::var("EXCHANGE_RATE_API_KEY")
14        .expect("Set EXCHANGE_RATE_API_KEY environment variable");
15
16    let client = ExchangeRateAPI::new(&api_key);
17
18    // -----------------------------------------------------------------------
19    // 1. Get latest rates
20    // -----------------------------------------------------------------------
21    println!("--- Latest Rates (USD) ---");
22    match client.latest("USD", Some("EUR,GBP,JPY,CAD")) {
23        Ok(resp) => {
24            for (currency, rate) in &resp.rates {
25                println!("  {} = {}", currency, rate);
26            }
27        }
28        Err(e) => eprintln!("Error: {}", e),
29    }
30
31    // -----------------------------------------------------------------------
32    // 2. Convert currency
33    // -----------------------------------------------------------------------
34    println!("\n--- Convert 100 USD to EUR ---");
35    match client.convert("USD", "EUR", 100.0) {
36        Ok(resp) => {
37            println!("  {} {} = {} {}", resp.amount, resp.from, resp.result, resp.to);
38            println!("  Rate: {}", resp.rate);
39        }
40        Err(e) => eprintln!("Error: {}", e),
41    }
42
43    // -----------------------------------------------------------------------
44    // 3. Get a single exchange rate
45    // -----------------------------------------------------------------------
46    println!("\n--- Single Rate: GBP/JPY ---");
47    match client.get_rate("GBP", "JPY") {
48        Ok(rate) => println!("  1 GBP = {} JPY", rate),
49        Err(e) => eprintln!("Error: {}", e),
50    }
51
52    // -----------------------------------------------------------------------
53    // 4. Historical rates for a specific date
54    // -----------------------------------------------------------------------
55    println!("\n--- Historical Rates (2025-01-15, EUR base) ---");
56    match client.for_date("2025-01-15", "EUR", Some("USD,GBP")) {
57        Ok(resp) => {
58            println!("  Date: {}", resp.date);
59            for (currency, rate) in &resp.rates {
60                println!("  {} = {}", currency, rate);
61            }
62        }
63        Err(e) => eprintln!("Error: {}", e),
64    }
65
66    // -----------------------------------------------------------------------
67    // 5. Time series
68    // -----------------------------------------------------------------------
69    println!("\n--- Time Series (USD/EUR, Jan 2025) ---");
70    match client.time_series("2025-01-01", "2025-01-07", "USD", Some("EUR")) {
71        Ok(resp) => {
72            let mut dates: Vec<&String> = resp.rates.keys().collect();
73            dates.sort();
74            for date in dates {
75                if let Some(rate) = resp.rates[date].get("EUR") {
76                    println!("  {}: EUR = {}", date, rate);
77                }
78            }
79        }
80        Err(e) => eprintln!("Error: {}", e),
81    }
82
83    // -----------------------------------------------------------------------
84    // 6. List all symbols
85    // -----------------------------------------------------------------------
86    println!("\n--- Supported Currencies (first 10) ---");
87    match client.symbols() {
88        Ok(resp) => {
89            let mut symbols: Vec<(&String, &String)> = resp.symbols.iter().collect();
90            symbols.sort_by_key(|(code, _)| code.to_owned());
91            for (code, name) in symbols.iter().take(10) {
92                println!("  {}: {}", code, name);
93            }
94            println!("  ... and {} more", resp.symbols.len().saturating_sub(10));
95        }
96        Err(e) => eprintln!("Error: {}", e),
97    }
98
99    // -----------------------------------------------------------------------
100    // 7. Historical rates with preset period
101    // -----------------------------------------------------------------------
102    println!("\n--- Last 7 Days: USD/EUR ---");
103    match client.get_historical_rates("USD", "EUR", Period::SevenDays) {
104        Ok(resp) => {
105            let mut dates: Vec<&String> = resp.rates.keys().collect();
106            dates.sort();
107            for date in dates {
108                if let Some(rate) = resp.rates[date].get("EUR") {
109                    println!("  {}: {}", date, rate);
110                }
111            }
112        }
113        Err(e) => eprintln!("Error: {}", e),
114    }
115}
Source

pub fn time_series( &self, start: &str, end: &str, base: &str, symbols: Option<&str>, ) -> Result<TimeSeriesResponse>

Fetches exchange rates over a date range (time series).

§Arguments
  • start - Start date in YYYY-MM-DD format (inclusive).
  • end - End date in YYYY-MM-DD format (inclusive).
  • base - The base currency code.
  • symbols - Optional comma-separated list of target currencies.
§Example
let client = ExchangeRateAPI::new("era_live_xxx");
let resp = client.time_series("2025-01-01", "2025-01-31", "USD", Some("EUR")).unwrap();
for (date, rates) in &resp.rates {
    println!("{}: EUR = {}", date, rates["EUR"]);
}
Examples found in repository?
examples/basic.rs (line 70)
11fn main() {
12    // Read API key from environment
13    let api_key = std::env::var("EXCHANGE_RATE_API_KEY")
14        .expect("Set EXCHANGE_RATE_API_KEY environment variable");
15
16    let client = ExchangeRateAPI::new(&api_key);
17
18    // -----------------------------------------------------------------------
19    // 1. Get latest rates
20    // -----------------------------------------------------------------------
21    println!("--- Latest Rates (USD) ---");
22    match client.latest("USD", Some("EUR,GBP,JPY,CAD")) {
23        Ok(resp) => {
24            for (currency, rate) in &resp.rates {
25                println!("  {} = {}", currency, rate);
26            }
27        }
28        Err(e) => eprintln!("Error: {}", e),
29    }
30
31    // -----------------------------------------------------------------------
32    // 2. Convert currency
33    // -----------------------------------------------------------------------
34    println!("\n--- Convert 100 USD to EUR ---");
35    match client.convert("USD", "EUR", 100.0) {
36        Ok(resp) => {
37            println!("  {} {} = {} {}", resp.amount, resp.from, resp.result, resp.to);
38            println!("  Rate: {}", resp.rate);
39        }
40        Err(e) => eprintln!("Error: {}", e),
41    }
42
43    // -----------------------------------------------------------------------
44    // 3. Get a single exchange rate
45    // -----------------------------------------------------------------------
46    println!("\n--- Single Rate: GBP/JPY ---");
47    match client.get_rate("GBP", "JPY") {
48        Ok(rate) => println!("  1 GBP = {} JPY", rate),
49        Err(e) => eprintln!("Error: {}", e),
50    }
51
52    // -----------------------------------------------------------------------
53    // 4. Historical rates for a specific date
54    // -----------------------------------------------------------------------
55    println!("\n--- Historical Rates (2025-01-15, EUR base) ---");
56    match client.for_date("2025-01-15", "EUR", Some("USD,GBP")) {
57        Ok(resp) => {
58            println!("  Date: {}", resp.date);
59            for (currency, rate) in &resp.rates {
60                println!("  {} = {}", currency, rate);
61            }
62        }
63        Err(e) => eprintln!("Error: {}", e),
64    }
65
66    // -----------------------------------------------------------------------
67    // 5. Time series
68    // -----------------------------------------------------------------------
69    println!("\n--- Time Series (USD/EUR, Jan 2025) ---");
70    match client.time_series("2025-01-01", "2025-01-07", "USD", Some("EUR")) {
71        Ok(resp) => {
72            let mut dates: Vec<&String> = resp.rates.keys().collect();
73            dates.sort();
74            for date in dates {
75                if let Some(rate) = resp.rates[date].get("EUR") {
76                    println!("  {}: EUR = {}", date, rate);
77                }
78            }
79        }
80        Err(e) => eprintln!("Error: {}", e),
81    }
82
83    // -----------------------------------------------------------------------
84    // 6. List all symbols
85    // -----------------------------------------------------------------------
86    println!("\n--- Supported Currencies (first 10) ---");
87    match client.symbols() {
88        Ok(resp) => {
89            let mut symbols: Vec<(&String, &String)> = resp.symbols.iter().collect();
90            symbols.sort_by_key(|(code, _)| code.to_owned());
91            for (code, name) in symbols.iter().take(10) {
92                println!("  {}: {}", code, name);
93            }
94            println!("  ... and {} more", resp.symbols.len().saturating_sub(10));
95        }
96        Err(e) => eprintln!("Error: {}", e),
97    }
98
99    // -----------------------------------------------------------------------
100    // 7. Historical rates with preset period
101    // -----------------------------------------------------------------------
102    println!("\n--- Last 7 Days: USD/EUR ---");
103    match client.get_historical_rates("USD", "EUR", Period::SevenDays) {
104        Ok(resp) => {
105            let mut dates: Vec<&String> = resp.rates.keys().collect();
106            dates.sort();
107            for date in dates {
108                if let Some(rate) = resp.rates[date].get("EUR") {
109                    println!("  {}: {}", date, rate);
110                }
111            }
112        }
113        Err(e) => eprintln!("Error: {}", e),
114    }
115}
Source

pub fn symbols(&self) -> Result<SymbolsResponse>

Lists all supported currency symbols.

§Example
let client = ExchangeRateAPI::new("era_live_xxx");
let resp = client.symbols().unwrap();
for (code, name) in &resp.symbols {
    println!("{}: {}", code, name);
}
Examples found in repository?
examples/basic.rs (line 87)
11fn main() {
12    // Read API key from environment
13    let api_key = std::env::var("EXCHANGE_RATE_API_KEY")
14        .expect("Set EXCHANGE_RATE_API_KEY environment variable");
15
16    let client = ExchangeRateAPI::new(&api_key);
17
18    // -----------------------------------------------------------------------
19    // 1. Get latest rates
20    // -----------------------------------------------------------------------
21    println!("--- Latest Rates (USD) ---");
22    match client.latest("USD", Some("EUR,GBP,JPY,CAD")) {
23        Ok(resp) => {
24            for (currency, rate) in &resp.rates {
25                println!("  {} = {}", currency, rate);
26            }
27        }
28        Err(e) => eprintln!("Error: {}", e),
29    }
30
31    // -----------------------------------------------------------------------
32    // 2. Convert currency
33    // -----------------------------------------------------------------------
34    println!("\n--- Convert 100 USD to EUR ---");
35    match client.convert("USD", "EUR", 100.0) {
36        Ok(resp) => {
37            println!("  {} {} = {} {}", resp.amount, resp.from, resp.result, resp.to);
38            println!("  Rate: {}", resp.rate);
39        }
40        Err(e) => eprintln!("Error: {}", e),
41    }
42
43    // -----------------------------------------------------------------------
44    // 3. Get a single exchange rate
45    // -----------------------------------------------------------------------
46    println!("\n--- Single Rate: GBP/JPY ---");
47    match client.get_rate("GBP", "JPY") {
48        Ok(rate) => println!("  1 GBP = {} JPY", rate),
49        Err(e) => eprintln!("Error: {}", e),
50    }
51
52    // -----------------------------------------------------------------------
53    // 4. Historical rates for a specific date
54    // -----------------------------------------------------------------------
55    println!("\n--- Historical Rates (2025-01-15, EUR base) ---");
56    match client.for_date("2025-01-15", "EUR", Some("USD,GBP")) {
57        Ok(resp) => {
58            println!("  Date: {}", resp.date);
59            for (currency, rate) in &resp.rates {
60                println!("  {} = {}", currency, rate);
61            }
62        }
63        Err(e) => eprintln!("Error: {}", e),
64    }
65
66    // -----------------------------------------------------------------------
67    // 5. Time series
68    // -----------------------------------------------------------------------
69    println!("\n--- Time Series (USD/EUR, Jan 2025) ---");
70    match client.time_series("2025-01-01", "2025-01-07", "USD", Some("EUR")) {
71        Ok(resp) => {
72            let mut dates: Vec<&String> = resp.rates.keys().collect();
73            dates.sort();
74            for date in dates {
75                if let Some(rate) = resp.rates[date].get("EUR") {
76                    println!("  {}: EUR = {}", date, rate);
77                }
78            }
79        }
80        Err(e) => eprintln!("Error: {}", e),
81    }
82
83    // -----------------------------------------------------------------------
84    // 6. List all symbols
85    // -----------------------------------------------------------------------
86    println!("\n--- Supported Currencies (first 10) ---");
87    match client.symbols() {
88        Ok(resp) => {
89            let mut symbols: Vec<(&String, &String)> = resp.symbols.iter().collect();
90            symbols.sort_by_key(|(code, _)| code.to_owned());
91            for (code, name) in symbols.iter().take(10) {
92                println!("  {}: {}", code, name);
93            }
94            println!("  ... and {} more", resp.symbols.len().saturating_sub(10));
95        }
96        Err(e) => eprintln!("Error: {}", e),
97    }
98
99    // -----------------------------------------------------------------------
100    // 7. Historical rates with preset period
101    // -----------------------------------------------------------------------
102    println!("\n--- Last 7 Days: USD/EUR ---");
103    match client.get_historical_rates("USD", "EUR", Period::SevenDays) {
104        Ok(resp) => {
105            let mut dates: Vec<&String> = resp.rates.keys().collect();
106            dates.sort();
107            for date in dates {
108                if let Some(rate) = resp.rates[date].get("EUR") {
109                    println!("  {}: {}", date, rate);
110                }
111            }
112        }
113        Err(e) => eprintln!("Error: {}", e),
114    }
115}
Source

pub fn get_rate(&self, from: &str, to: &str) -> Result<f64>

Gets the exchange rate for a single currency pair.

This is a convenience wrapper around latest that returns just the rate as an f64.

§Arguments
  • from - Source currency code (e.g. "USD").
  • to - Target currency code (e.g. "EUR").
§Example
let client = ExchangeRateAPI::new("era_live_xxx");
let rate = client.get_rate("USD", "EUR").unwrap();
println!("1 USD = {} EUR", rate);
Examples found in repository?
examples/basic.rs (line 47)
11fn main() {
12    // Read API key from environment
13    let api_key = std::env::var("EXCHANGE_RATE_API_KEY")
14        .expect("Set EXCHANGE_RATE_API_KEY environment variable");
15
16    let client = ExchangeRateAPI::new(&api_key);
17
18    // -----------------------------------------------------------------------
19    // 1. Get latest rates
20    // -----------------------------------------------------------------------
21    println!("--- Latest Rates (USD) ---");
22    match client.latest("USD", Some("EUR,GBP,JPY,CAD")) {
23        Ok(resp) => {
24            for (currency, rate) in &resp.rates {
25                println!("  {} = {}", currency, rate);
26            }
27        }
28        Err(e) => eprintln!("Error: {}", e),
29    }
30
31    // -----------------------------------------------------------------------
32    // 2. Convert currency
33    // -----------------------------------------------------------------------
34    println!("\n--- Convert 100 USD to EUR ---");
35    match client.convert("USD", "EUR", 100.0) {
36        Ok(resp) => {
37            println!("  {} {} = {} {}", resp.amount, resp.from, resp.result, resp.to);
38            println!("  Rate: {}", resp.rate);
39        }
40        Err(e) => eprintln!("Error: {}", e),
41    }
42
43    // -----------------------------------------------------------------------
44    // 3. Get a single exchange rate
45    // -----------------------------------------------------------------------
46    println!("\n--- Single Rate: GBP/JPY ---");
47    match client.get_rate("GBP", "JPY") {
48        Ok(rate) => println!("  1 GBP = {} JPY", rate),
49        Err(e) => eprintln!("Error: {}", e),
50    }
51
52    // -----------------------------------------------------------------------
53    // 4. Historical rates for a specific date
54    // -----------------------------------------------------------------------
55    println!("\n--- Historical Rates (2025-01-15, EUR base) ---");
56    match client.for_date("2025-01-15", "EUR", Some("USD,GBP")) {
57        Ok(resp) => {
58            println!("  Date: {}", resp.date);
59            for (currency, rate) in &resp.rates {
60                println!("  {} = {}", currency, rate);
61            }
62        }
63        Err(e) => eprintln!("Error: {}", e),
64    }
65
66    // -----------------------------------------------------------------------
67    // 5. Time series
68    // -----------------------------------------------------------------------
69    println!("\n--- Time Series (USD/EUR, Jan 2025) ---");
70    match client.time_series("2025-01-01", "2025-01-07", "USD", Some("EUR")) {
71        Ok(resp) => {
72            let mut dates: Vec<&String> = resp.rates.keys().collect();
73            dates.sort();
74            for date in dates {
75                if let Some(rate) = resp.rates[date].get("EUR") {
76                    println!("  {}: EUR = {}", date, rate);
77                }
78            }
79        }
80        Err(e) => eprintln!("Error: {}", e),
81    }
82
83    // -----------------------------------------------------------------------
84    // 6. List all symbols
85    // -----------------------------------------------------------------------
86    println!("\n--- Supported Currencies (first 10) ---");
87    match client.symbols() {
88        Ok(resp) => {
89            let mut symbols: Vec<(&String, &String)> = resp.symbols.iter().collect();
90            symbols.sort_by_key(|(code, _)| code.to_owned());
91            for (code, name) in symbols.iter().take(10) {
92                println!("  {}: {}", code, name);
93            }
94            println!("  ... and {} more", resp.symbols.len().saturating_sub(10));
95        }
96        Err(e) => eprintln!("Error: {}", e),
97    }
98
99    // -----------------------------------------------------------------------
100    // 7. Historical rates with preset period
101    // -----------------------------------------------------------------------
102    println!("\n--- Last 7 Days: USD/EUR ---");
103    match client.get_historical_rates("USD", "EUR", Period::SevenDays) {
104        Ok(resp) => {
105            let mut dates: Vec<&String> = resp.rates.keys().collect();
106            dates.sort();
107            for date in dates {
108                if let Some(rate) = resp.rates[date].get("EUR") {
109                    println!("  {}: {}", date, rate);
110                }
111            }
112        }
113        Err(e) => eprintln!("Error: {}", e),
114    }
115}
Source

pub fn get_historical_rates( &self, source: &str, target: &str, period: Period, ) -> Result<TimeSeriesResponse>

Gets historical rates for a preset time period.

This is a convenience method that calculates the appropriate start and end dates and calls time_series.

§Arguments
  • source - Base currency code (e.g. "USD").
  • target - Target currency code (e.g. "EUR").
  • period - One of the preset Period values.
§Example
let client = ExchangeRateAPI::new("era_live_xxx");
let resp = client.get_historical_rates("USD", "EUR", Period::SevenDays).unwrap();
for (date, rates) in &resp.rates {
    println!("{}: {}", date, rates["EUR"]);
}
Examples found in repository?
examples/basic.rs (line 103)
11fn main() {
12    // Read API key from environment
13    let api_key = std::env::var("EXCHANGE_RATE_API_KEY")
14        .expect("Set EXCHANGE_RATE_API_KEY environment variable");
15
16    let client = ExchangeRateAPI::new(&api_key);
17
18    // -----------------------------------------------------------------------
19    // 1. Get latest rates
20    // -----------------------------------------------------------------------
21    println!("--- Latest Rates (USD) ---");
22    match client.latest("USD", Some("EUR,GBP,JPY,CAD")) {
23        Ok(resp) => {
24            for (currency, rate) in &resp.rates {
25                println!("  {} = {}", currency, rate);
26            }
27        }
28        Err(e) => eprintln!("Error: {}", e),
29    }
30
31    // -----------------------------------------------------------------------
32    // 2. Convert currency
33    // -----------------------------------------------------------------------
34    println!("\n--- Convert 100 USD to EUR ---");
35    match client.convert("USD", "EUR", 100.0) {
36        Ok(resp) => {
37            println!("  {} {} = {} {}", resp.amount, resp.from, resp.result, resp.to);
38            println!("  Rate: {}", resp.rate);
39        }
40        Err(e) => eprintln!("Error: {}", e),
41    }
42
43    // -----------------------------------------------------------------------
44    // 3. Get a single exchange rate
45    // -----------------------------------------------------------------------
46    println!("\n--- Single Rate: GBP/JPY ---");
47    match client.get_rate("GBP", "JPY") {
48        Ok(rate) => println!("  1 GBP = {} JPY", rate),
49        Err(e) => eprintln!("Error: {}", e),
50    }
51
52    // -----------------------------------------------------------------------
53    // 4. Historical rates for a specific date
54    // -----------------------------------------------------------------------
55    println!("\n--- Historical Rates (2025-01-15, EUR base) ---");
56    match client.for_date("2025-01-15", "EUR", Some("USD,GBP")) {
57        Ok(resp) => {
58            println!("  Date: {}", resp.date);
59            for (currency, rate) in &resp.rates {
60                println!("  {} = {}", currency, rate);
61            }
62        }
63        Err(e) => eprintln!("Error: {}", e),
64    }
65
66    // -----------------------------------------------------------------------
67    // 5. Time series
68    // -----------------------------------------------------------------------
69    println!("\n--- Time Series (USD/EUR, Jan 2025) ---");
70    match client.time_series("2025-01-01", "2025-01-07", "USD", Some("EUR")) {
71        Ok(resp) => {
72            let mut dates: Vec<&String> = resp.rates.keys().collect();
73            dates.sort();
74            for date in dates {
75                if let Some(rate) = resp.rates[date].get("EUR") {
76                    println!("  {}: EUR = {}", date, rate);
77                }
78            }
79        }
80        Err(e) => eprintln!("Error: {}", e),
81    }
82
83    // -----------------------------------------------------------------------
84    // 6. List all symbols
85    // -----------------------------------------------------------------------
86    println!("\n--- Supported Currencies (first 10) ---");
87    match client.symbols() {
88        Ok(resp) => {
89            let mut symbols: Vec<(&String, &String)> = resp.symbols.iter().collect();
90            symbols.sort_by_key(|(code, _)| code.to_owned());
91            for (code, name) in symbols.iter().take(10) {
92                println!("  {}: {}", code, name);
93            }
94            println!("  ... and {} more", resp.symbols.len().saturating_sub(10));
95        }
96        Err(e) => eprintln!("Error: {}", e),
97    }
98
99    // -----------------------------------------------------------------------
100    // 7. Historical rates with preset period
101    // -----------------------------------------------------------------------
102    println!("\n--- Last 7 Days: USD/EUR ---");
103    match client.get_historical_rates("USD", "EUR", Period::SevenDays) {
104        Ok(resp) => {
105            let mut dates: Vec<&String> = resp.rates.keys().collect();
106            dates.sort();
107            for date in dates {
108                if let Some(rate) = resp.rates[date].get("EUR") {
109                    println!("  {}: {}", date, rate);
110                }
111            }
112        }
113        Err(e) => eprintln!("Error: {}", e),
114    }
115}

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more