| Crates.io | vec-string-to-static-str |
| lib.rs | vec-string-to-static-str |
| version | 1.0.0 |
| created_at | 2024-06-06 04:13:01.448602+00 |
| updated_at | 2024-06-06 04:13:01.448602+00 |
| description | A Rust library providing utilities for converting vectors of `String`s into vectors of `&'static str`. |
| homepage | |
| repository | https://github.com/mcclementines/vec-string-to-static-str |
| max_upload_size | |
| id | 1263392 |
| size | 10,201 |
vec-string-to-static-str is a Rust library providing utilities for converting vectors of Strings into vectors of &'static str.
This library includes both safe and unsafe methods for achieving this conversion.
String to &'static str with Box::leakString to &'static str using std::mem::transmuteAdd vec_string_to_static_str to your Cargo.toml:
[dependencies]
vec-string-to-static-str = "1.0.0"
Make sure to add the "unsafe" feature flag to enable
unsafe_vec_string_to_static_str if needed.
use vec_string_to_static_str::{vec_string_to_static_str, unsafe_vec_string_to_static_str};
fn main() {
let strings = vec![String::from("hello"), String::from("world")];
// Safe method
let static_strs = vec_string_to_static_str(&strings);
assert_eq!(static_strs, vec!["hello", "world"]);
// Unsafe method
let unsafe_static_strs = unsafe_vec_string_to_static_str(&strings);
assert_eq!(unsafe_static_strs, vec!["hello", "world"]);
}
Box::leak to convert String to &'static str, which leaks memory.std::mem::transmute to extend the lifetime of string slices, which can cause undefined behavior if not used correctly.This project is licensed under the MIT License. See the LICENSE file for details.