Crates.io | super-struct |
lib.rs | super-struct |
version | 1.0.2 |
source | src |
created_at | 2024-06-30 07:28:32.849447 |
updated_at | 2024-07-02 04:00:39.574442 |
description | use the `struct` of `Rust` in the same way that `dict` of `Python` |
homepage | https://github.com/Web-Coke/super-struct |
repository | https://github.com/Web-Coke/super-struct |
max_upload_size | |
id | 1287816 |
size | 39,186 |
💟💟💟小奕💟💟💟
super-struct
旨在帮助开发者像 Python
的 dict
那样使用 Rust
的 struct
The super-struct
is designed to help developers use the struct
of Rust
in the same way that dict
of Python
目前这个库的大部分功能已经实现
Most of the functionality of this library has already been implemented
还有一部分功能正在积极开发中
There are also some features that are under active development
struct
中的字段名(keys)struct
中的值struct
的字段名称(key)获取值struct
的字段名称(key)设置值struct
中添加字段(key)(这个应该很难做到)struct
struct
struct
struct
struct
(this should be hard to do)如果你有更好的提议或需求欢迎提issue
If you have a better proposal or need, please feel free to raise an issue
在Cargo.toml中添加
Add in Cargo.toml
[dependencies]
super-struct = "*"
struct
里的类型都一致的情况下
struct
is the case where the types are the same
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]
的语法糖不可用
If the types in struct
are inconsistent, the syntactic sugar for Self[key]
is not available
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"
}