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
impl ExchangeRateAPI
Sourcepub fn new(api_key: &str) -> Self
pub fn new(api_key: &str) -> Self
Creates a new ExchangeRateAPI client.
§Arguments
api_key- Your API key (format:era_live_...). Obtain one at https://exchange-rateapi.com.
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}Sourcepub fn latest(
&self,
base: &str,
symbols: Option<&str>,
) -> Result<LatestResponse>
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")). PassNoneto 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}Sourcepub fn convert(
&self,
from: &str,
to: &str,
amount: f64,
) -> Result<ConvertResponse>
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}Sourcepub fn for_date(
&self,
date: &str,
base: &str,
symbols: Option<&str>,
) -> Result<HistoricalResponse>
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 inYYYY-MM-DDformat.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}Sourcepub fn time_series(
&self,
start: &str,
end: &str,
base: &str,
symbols: Option<&str>,
) -> Result<TimeSeriesResponse>
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 inYYYY-MM-DDformat (inclusive).end- End date inYYYY-MM-DDformat (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}Sourcepub fn symbols(&self) -> Result<SymbolsResponse>
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}Sourcepub fn get_rate(&self, from: &str, to: &str) -> Result<f64>
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}Sourcepub fn get_historical_rates(
&self,
source: &str,
target: &str,
period: Period,
) -> Result<TimeSeriesResponse>
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 presetPeriodvalues.
§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§
impl Freeze for ExchangeRateAPI
impl !RefUnwindSafe for ExchangeRateAPI
impl Send for ExchangeRateAPI
impl Sync for ExchangeRateAPI
impl Unpin for ExchangeRateAPI
impl UnsafeUnpin for ExchangeRateAPI
impl !UnwindSafe for ExchangeRateAPI
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more