2个稳定版本
| 1.0.2 | 2024年7月2日 | 
|---|---|
| 1.0.1 | 2024年6月30日 | 
| 0.1.0 |  | 
#529 in 数据结构
每月346次下载
17KB
349 行
💟💟💟小奕💟💟💟
文档
super-struct旨在帮助开发者像使用Python的dict一样使用Rust的struct
设计super-struct是为了帮助开发者像使用Python的dict一样使用Rust的struct
该库的大部分功能已经实现
该库的大部分功能已经实现
还有一部分功能正在积极开发中
还有一部分功能正在积极开发中
- 获取struct中的字段名(keys)
- 获取struct中的值
- 根据struct的字段名称(key)获取值
- 根据struct的字段名称(key)设置值
- 动态向struct中添加字段(key)(这应该很难做到)
- 获取struct中的字段名(keys)
- 获取struct中的值
- 根据struct的字段名称(key)获取值
- 根据struct的字段名称(key)设置值
- 动态向struct中添加字段(key)(这应该很难做到)
如果您有更好的建议或需求,欢迎提出issue
如果您有更好的建议或需求,请随时提出issue
示例
在Cargo.toml中添加
在Cargo.toml中添加
[dependencies]
super-struct = "*"
当struct里的类型都一致时
当struct中的类型一致时
use super_struct::*;
#[derive(Debug, Rustdict)]
struct Test {
    name: String,
    country: String,
    language: String,
}
fn main() {
    let mut test = Test {
        name: "WebChang".to_string(),
        country: "China".to_string(),
        language: "Mandarin".to_string(),
    };
    println!("{:?}", test.keys());
    // ["name", "country", "language"]
    for i in test.keys() {
        test[i] = "Hello".to_string();
        if i == "language" {
            test[i] = "Rust".to_string()
        }
    }
    println!("{:?}", test);
    // Test { name: "Hello", country: "Hello", language: "Rust" }
    test.set("country", "中国".to_string());
    println!("{:?}", test.values());
    // ["Hello", "中国", "Rust"]
    test["country"] = test.get("language").clone();
    println!("{:?}", test.values());
    //["Hello", "Rust", "Rust"]
}
如果 struct 中的类型不一致,则 Self[key] 的语法糖不可用
如果 struct 中的类型不一致,则 Self[key] 的语法糖不可用
use super_struct::*;
#[derive(Debug, Rustdict)]
struct Test {
    name: String,
    country: String,
    language: String,
    age: u8,
}
fn main() {
    let mut test = Test {
        name: "WebChang".to_string(),
        country: "China".to_string(),
        language: "Mandarin".to_string(),
        age: 24u8,
    };
    println!("{:?}", test.keys());
    // ["name", "country", "language", "age"]
    println!("{:?}", test.values());
    // [Any { .. }, Any { .. }, Any { .. }, Any { .. }]
    for i in test.keys() {
        test.set(i, &"Hello".to_string());
        // 如果类型不一致则什么都不会发生
        // If the types are inconsistent, nothing happens
        // if i == &"age" {
        //     test.set(i, &25u8)
        // }
    }
    println!("{:?}", test);
    // Test { name: "Hello", country: "Hello", language: "Hello", age: 24 }
    test.set("age", &25u8);
    println!("{:?}", test);
    // Test { name: "Hello", country: "Hello", language: "Hello", age: 25 }
    println!(
        "{:?}",
        test.get("language").downcast_ref::<String>().unwrap()
    );
    // "Hello"
}
更新日志
0.1.0
- 初始版本
- 首次发布
1.0.0 & 1.0.1
- 更正文档错误
- 纠正文档错误
1.0.2
依赖项
约 270-720KB
约 17K SLoC