Skip to content

A Rust crate of macros for easily creating String types

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

neoncitylights/string-literals

Repository files navigation

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.

About

A Rust crate of macros for easily creating String types

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Sponsor this project

Languages