vec-string-to-static-str

Crates.iovec-string-to-static-str
lib.rsvec-string-to-static-str
version1.0.0
sourcesrc
created_at2024-06-06 04:13:01.448602
updated_at2024-06-06 04:13:01.448602
descriptionA Rust library providing utilities for converting vectors of `String`s into vectors of `&'static str`.
homepage
repositoryhttps://github.com/mcclementines/vec-string-to-static-str
max_upload_size
id1263392
size10,201
Jonathan McClement (mcclementines)

documentation

README

vec-string-to-static-str

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.

Features

  • Safe conversion of String to &'static str with Box::leak
  • Unsafe conversion of String to &'static str using std::mem::transmute

Usage

Add 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.

Example

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

Safety

  • Safe Method: Uses Box::leak to convert String to &'static str, which leaks memory.
  • Unsafe Method: Uses std::mem::transmute to extend the lifetime of string slices, which can cause undefined behavior if not used correctly.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Commit count: 2

cargo fmt