pub struct WordInput {
pub text: String,
pub weight: f32,
}Fields§
§text: String§weight: f32Implementations§
Source§impl WordInput
impl WordInput
Sourcepub fn new(text: impl Into<String>, weight: f32) -> Self
pub fn new(text: impl Into<String>, weight: f32) -> Self
Examples found in repository?
examples/advanced.rs (line 6)
4fn main() -> Result<(), Box<dyn std::error::Error>> {
5 let words = vec![
6 WordInput::new("Custom", 90.0),
7 WordInput::new("Colors", 80.0),
8 WordInput::new("Seed", 70.0),
9 WordInput::new("Fixed", 60.0),
10 WordInput::new("Layout", 50.0),
11 ];
12
13 let wordcloud = WordCloudBuilder::new()
14 .size(600, 400)
15 .background("#1a1a1a")
16 .colors(vec!["#FF0000", "#00FF00", "#0000FF", "#FFFF00", "#00FFFF"])
17 .seed(42)
18 .angles(vec![0.0])
19 .font_size_range(20.0, 100.0)
20 .build(&words)?;
21
22 fs::write("output_advanced.png", wordcloud.to_png(1.0)?)?;
23 println!("Generated advanced word cloud: output_advanced.png");
24
25 Ok(())
26}More examples
examples/mask_shape.rs (line 13)
4fn main() -> Result<(), Box<dyn std::error::Error>> {
5 let output_path = "output_mask_heart.png";
6
7 let width = 800;
8 let height = 800;
9
10 println!("Generating words...");
11 let mut words = Vec::new();
12 for i in 0..300 {
13 words.push(WordInput::new("Love", 100.0));
14 words.push(WordInput::new("Rust", 80.0));
15 words.push(WordInput::new("Heart", 60.0 + (i as f32 % 40.0)));
16 words.push(WordInput::new("Mask", 40.0));
17 }
18
19 println!("Building word cloud...");
20
21 let scheme = ColorScheme::Default;
22 let wordcloud = WordCloudBuilder::new()
23 .size(width, height)
24 .mask_preset(MaskShape::Heart)
25 .color_scheme(scheme)
26 .background(scheme.background_color())
27 .padding(2)
28 .word_spacing(2.0)
29 .font_size_range(12.0, 80.0)
30 .build(&words)?;
31
32 println!("Saving to {}...", output_path);
33 fs::write(output_path, wordcloud.to_png(2.0)?)?;
34
35 println!("Done! Check {}", output_path);
36 Ok(())
37}examples/chinese_dense.rs (line 73)
54fn generate_dense_chinese_data() -> Vec<WordInput> {
55 let mut words = Vec::new();
56 let mut rng = rand::rng();
57
58 // 核心超大词,权重分布更均匀,突出重点词汇,同时避免权重集中
59 let core_keywords = vec![
60 "Rust编程",
61 "高性能",
62 "内存安全",
63 "WebAssembly",
64 "系统级",
65 "无GC",
66 "所有权",
67 "生命周期",
68 "借用检查",
69 "并发模型",
70 ];
71 for w in &core_keywords {
72 // 在85~100之间随机,增加大词的字体差异感
73 words.push(WordInput::new(*w, rng.random_range(85.0..=100.0)));
74 }
75
76 // 重要相关概念,权重调整为55~80,增强视觉层次感
77 let concepts = vec![
78 "Cargo",
79 "Crates.io",
80 "Tokio",
81 "Actix",
82 "Serde",
83 "Diesel",
84 "Async/Await",
85 "Trait",
86 "Struct",
87 "Enum",
88 "Pattern Matching",
89 "Zero-cost",
90 "安全性",
91 "跨平台",
92 "嵌入式",
93 "网络服务",
94 "命令行工具",
95 "错误处理",
96 "Result",
97 "Option",
98 ];
99 for w in &concepts {
100 words.push(WordInput::new(*w, rng.random_range(55.0..=80.0)));
101 }
102
103 // 常用基础词汇,分三轮加入,权重均匀分布40~55,避免视觉“拥挤”
104 let common = vec![
105 "编译",
106 "运行",
107 "测试",
108 "文档",
109 "模块",
110 "函数",
111 "闭包",
112 "迭代器",
113 "集合",
114 "字符串",
115 "指针",
116 "引用",
117 "智能指针",
118 "Box",
119 "Rc",
120 "Arc",
121 "Mutex",
122 "Channel",
123 "Future",
124 "Stream",
125 "宏定义",
126 "属性",
127 "泛型",
128 "Community",
129 "Foundation",
130 "Docs",
131 "Book",
132 "Examples",
133 "Tutorial",
134 ];
135 for _ in 0..3 {
136 for w in &common {
137 words.push(WordInput::new(*w, rng.random_range(40.0..=55.0)));
138 }
139 }
140
141 // 填充词汇,丰富词云细节,权重范围调整为20~35,减少过密感
142 let fillers = vec![
143 "代码", "逻辑", "数据", "接口", "实现", "调用", "返回", "参数", "类型", "变量", "常量",
144 "静态", "动态", "链接", "库", "包", "依赖", "版本", "发布", "构建", "优化", "调试", "日志",
145 "监控", "设计", "架构", "模式", "算法", "结构", "效率", "速度", "稳定", "扩展", "维护",
146 "重构", "迁移", "学习", "曲线", "入门", "精通",
147 ];
148 for _ in 0..8 {
149 // 略减少次数,控制填充词数量避免拥挤
150 for w in &fillers {
151 words.push(WordInput::new(*w, rng.random_range(20.0..=35.0)));
152 }
153 }
154
155 words
156}Trait Implementations§
Auto Trait Implementations§
impl Freeze for WordInput
impl RefUnwindSafe for WordInput
impl Send for WordInput
impl Sync for WordInput
impl Unpin for WordInput
impl UnwindSafe for WordInput
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more