#struct-fields #format-string #string #struct #replace #format #fmt


Easy access of struct fields in strings using different/custom pre/postfix: "Hello, {field}"

5 releases

0.1.4 Nov 11, 2023
0.1.3 Sep 19, 2022
0.1.2 Sep 14, 2022
0.1.1 Sep 12, 2022
0.1.0 Sep 12, 2022

#48 in Value formatting

Download history 1/week @ 2024-02-19 8/week @ 2024-02-26 2/week @ 2024-03-11 119/week @ 2024-03-25

121 downloads per month
Used in 2 crates (via querio)


68 lines

Easy access to struct fields in strings

🐠 add strung to the dependencies in the Cargo.toml:

strung = "0.1"

🦀 use/import everything of the prelude in rust:

use strung::prelude::*;

🦊 works for unnamed fields:

struct Struct (&'static str, u32);
fn main(){
    let s: String = Struct("Bob", 10).strung("{0} is {1}th"); 

🦊 works for named fields:

struct Struct {
    name: &'static str,
    pos: u32,
fn main(){
    let s: String = Struct{name:"Bob", pos:10}.strung("{name} is {pos}th."); 
    // fields can also be addressed by their index
    let z: String = Struct{name:"Bob", pos:10}.strung("{0} is {1}th."); 


🐳 use #[igno], #[ignore],#[strung(igno)] or #[strung(ignore)] to make a field unavailable
🦞 if a field type doesn't implement Display, it has to be ignored!

struct NoDisplay;
struct Struct (&'static str, u32, #[igno] NoDisplay);
fn main(){
    let s: String = Struct("Bob", 10, NoDisplay)
        .strung("{0} is {1}th, he won {2}!"); 


🐳 use #[cscd], #[cascade], #[strung(cscd)] or #[strung(cascade)] to cascade (recursion).
🐑 cascaded fields are ignored by default
🐔 use #[notice], #[ntce] or inside #[strung(..)] to make them available,

struct Struct &'static str, u32);
struct Cascade (u32, #[cscd] Struct);
fn main(){
    let s: String = Cascade(11,Struct("Bob", 10))
        .strung("{1.0} is {1.1}th for the {0}th time!"); 
    // => Bob is 10th for the 11th time!

Prefix and Postix Prefabs

🐈 5 different prefabs are provided:

struct Struct (&'static str, u32);
fn main(){
    let t = Struct("Bob", 10);
    let s = t.strung_curly("{0} is {1}th."); 
    let s = t.strung_angle("<0> is <1>th."); 
    let s = t.strung_dollry("${0} is ${1}th."); 
    let s = t.strung_dollar("$0 is $1th."); 
    let s = t.strung_hashtag("#0 is #1th."); 

Custom Prefix and Postix

🦊 you can also customize pre-/postfixes in different ways
🦅 globally - using static variables and .strung_static(..):

struct Struct (&'static str, u32);
fn main(){
    let s: String = Struct("Bob", 10).strung_static("<0> is <1>th."); 

🐎 per struct - this overrides the default .strung(..) pre/postfix:

struct Struct (&'static str, u32);
fn main(){
    let s: String = Struct("Bob", 10).strung("<0> is <1>th."); 

🐍 per call - using parameters .strung_dynamic(pre,post,..):

struct Struct (&'static str, u32);
fn main(){
    let s: String = Struct("Bob", 10).strung_dynamic("<",">","<0> is <1>th."); 

🦎 per call - using generic const chars .strung_generic::<pre,post>(..):

struct Struct (&'static str, u32);
fn main(){
    let s: String = Struct("Bob", 10).strung_generic::<'<','>'>("<0> is <1>th."); 

Performance Comparison

🐕 dynamic/generic/global have equal runtime speed
🐇 default/prefabs/per-struct are faster!
🐁 Using a string of ~650 chracters and 6 field placeholders:

More Information

🦕 Documentation
🦎 Changelog
🐱 GitHub
👾 Discord Server


Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.


~19K SLoC