string-literals

Crates.iostring-literals
lib.rsstring-literals
version1.0.1
sourcesrc
created_at2024-05-15 17:57:05.460636
updated_at2024-05-17 06:51:49.846784
descriptionRust macros to more easily create String types
homepage
repositoryhttps://github.com/neoncitylights/string-literals
max_upload_size
id1241324
size21,274
Samantha (neoncitylights)

documentation

README

string-literals

License nightly docs docs.rs CI Codecov

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:

  • pass in the literal to String::from(),
  • call .to_owned(), or .to_string() on the &str literal

This crate aims to make this slightly more ergonomic; see examples below in the Usage section.

Install

cargo add string-literals

Usage

Strings

use string_literals::s;

let old = "Hello, world!".to_owned();
let new = s!("Hello, world!");

Arrays, vectors

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"];

Hash maps

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",
};

License

Licensed under either of

at your option.

Contribution

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.

Commit count: 7

cargo fmt