Crates.io | string-literals |
lib.rs | string-literals |
version | 1.0.1 |
source | src |
created_at | 2024-05-15 17:57:05.460636 |
updated_at | 2024-05-17 06:51:49.846784 |
description | Rust macros to more easily create String types |
homepage | |
repository | https://github.com/neoncitylights/string-literals |
max_upload_size | |
id | 1241324 |
size | 21,274 |
A very tiny crate with Rust macros to more easily create String types.
When creating string literals in Rust, the given type is &str
. To create an owned String
type,
you either need to:
String::from()
,.to_owned()
, or .to_string()
on the &str
literalThis crate aims to make this slightly more ergonomic; see examples below in the Usage section.
cargo add string-literals
use string_literals::s;
let old = "Hello, world!".to_owned();
let new = s!("Hello, world!");
use string_literals::{string_arr, string_vec};
let old_arr: [String; 3] = ["Banana".to_owned(), "Apple".to_owned(), "Orange".to_owned()];
let new_arr: [String; 3] = string_arr!["Banana", "Apple", "Orange"];
let old_vec = vec!["Banana".to_owned(), "Apple".to_owned(), "Orange".to_owned()];
let new_vec = string_vec!["Banana", "Apple", "Orange"];
use std::collections::HashMap;
use string_literals::string_hashmap;
let mut old1: HashMap<String, String> = HashMap::new();
old1.insert("Banana".to_owned(), "Good".to_owned());
old1.insert("Apple".to_owned(), "Okay".to_owned());
let old2: HashMap<String, String> = HashMap::from([
("Banana".to_owned(), "Good".to_owned()),
("Apple".to_owned(), "Okay".to_owned()),
]);
let new: HashMap<String, String> = string_hashmap! {
"Banana" => "Good",
"Apple" => "Okay",
};
Licensed under either of
LICENSE-APACHE
or http://www.apache.org/licenses/LICENSE-2.0)LICENSE-MIT
or http://opensource.org/licenses/MIT)at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.