-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
94 additions
and
25 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
name: Rust CI | ||
|
||
on: | ||
push: | ||
branches: [ main ] | ||
pull_request: | ||
branches: [ main ] | ||
|
||
env: | ||
CARGO_TERM_COLOR: always | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: Build | ||
run: cargo build --verbose | ||
- name: Run tests | ||
run: cargo test --verbose | ||
- name: Run examples | ||
run: cargo run --example basic_usage |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
use stringvec::stringvec; | ||
|
||
fn main() { | ||
let mixed_types = stringvec!["hello", 42, 'A', 3.5]; | ||
println!("Mixed types vector: {:?}", mixed_types); | ||
|
||
let names = stringvec!["Alice", "Bob", "Charlie"]; | ||
println!("Names: {:?}", names); | ||
|
||
// Using with variables | ||
let age = 30; | ||
let height = 5.9; | ||
let info = stringvec!["John Doe", age, height]; | ||
println!("Person info: {:?}", info); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,63 @@ | ||
//! # stringvec | ||
//! | ||
//! `stringvec` is a Rust library that provides a macro for easily creating | ||
//! a `Vec<String>` from various types, along with a utility function to check | ||
//! if a value is a `String`. | ||
|
||
use std::any::{Any, TypeId}; | ||
|
||
#[macro_export] | ||
macro_rules! stringvec { | ||
($($element:expr),* $(,)?) => { | ||
vec![$($element.to_string()),*] | ||
}; | ||
} | ||
/// Creates a `Vec<String>` from a list of expressions. | ||
/// | ||
/// This macro converts each expression to a `String` using the `to_string()` | ||
/// method and collects them into a `Vec<String>`. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// use stringvec::stringvec; | ||
/// | ||
/// let words = stringvec!["hello", 42, 'A', 3.14]; | ||
/// assert_eq!(words, vec!["hello", "42", "A", "3.14"]); | ||
/// ``` | ||
#[macro_export] | ||
macro_rules! stringvec { | ||
($($element:expr),* $(,)?) => { | ||
vec![$($element.to_string()),*] | ||
}; | ||
} | ||
|
||
pub fn is_string(s: &dyn Any) -> bool { | ||
TypeId::of::<String>() == s.type_id() | ||
} | ||
/// Checks if the provided value is a `String`. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// use stringvec::is_string; | ||
/// | ||
/// let s = "hello".to_string(); | ||
/// let i = 42; | ||
/// | ||
/// assert!(is_string(&s)); | ||
/// assert!(!is_string(&i)); | ||
/// ``` | ||
pub fn is_string(s: &dyn Any) -> bool { | ||
TypeId::of::<String>() == s.type_id() | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_svec_macro() { | ||
let words = stringvec!["cat", 11, 'A', 3.14]; | ||
assert_eq!(words, vec!["cat", "11", "A", "3.14"]); | ||
} | ||
#[test] | ||
fn test_stringvec_macro() { | ||
let words = stringvec!["cat", 11, 'A', 3.5]; | ||
assert_eq!(words, vec!["cat", "11", "A", "3.5"]); | ||
} | ||
|
||
#[test] | ||
fn test_is_string() { | ||
let s = "Hello".to_string(); | ||
let i = 42; | ||
assert!(is_string(&s)); | ||
assert!(!is_string(&i)); | ||
} | ||
} | ||
#[test] | ||
fn test_is_string() { | ||
let s = "Hello".to_string(); | ||
let i = 42; | ||
assert!(is_string(&s)); | ||
assert!(!is_string(&i)); | ||
} | ||
} |