Skip to content

Commit ea9f26a

Browse files
Merge pull request #4 from rossfletcher19:UpdateFunctions
UpdateFunctions const and other UI updates.
2 parents 73f5982 + 17bc022 commit ea9f26a

5 files changed

Lines changed: 300 additions & 105 deletions

File tree

src/app.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ impl eframe::App for TemplateApp {
7272
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
7373
// Put your widgets into a `SidePanel`, `TopBottomPanel`, `CentralPanel`, `Window` or `Area`.
7474
// For inspiration and more examples, go to https://emilk.github.io/egui
75+
76+
// let mut style = (*ctx.style()).clone();
77+
// style.visuals.override_text_color = Some(Color32::from_rgb(183, 65, 14));
78+
// ctx.set_style(style);
79+
7580
if self.first_frame {
7681
ctx.set_visuals(egui::Visuals::light());
7782
self.first_frame = false;

src/central_panel.rs

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use eframe::egui;
2+
use egui::RichText;
23

34
fn increment(counter: &mut i32) {
45
*counter += 1;
@@ -26,13 +27,21 @@ pub fn central_panel_ui(
2627
show_area3: &mut bool,
2728
) {
2829
egui::CentralPanel::default().show(ctx, |ui| {
29-
ui.heading("rust_eframe_egui project, a learning project following along and building with the Rust book. Find the Rust Book Below!");
30+
ui.horizontal(|ui|{
31+
ui.add(
32+
egui::Image::new(egui::include_image!("../assets/Rust_logo_animated1.gif"))
33+
.fit_to_exact_size(egui::vec2(50.0, 50.0))
34+
.corner_radius(5),
35+
);
36+
ui.heading(RichText::new("rust_eframe_egui project, a learning project following along and building with the Rust book. Find the Rust Book Below!")
37+
.color(egui::Color32::from_rgb(183, 65, 14))
38+
.size(18.0)
39+
);
40+
});
3041

31-
ui.hyperlink_to("The Rust Programming Language book", "https://doc.rust-lang.org/stable/book");
42+
let mut theme = egui_extras::syntax_highlighting::CodeTheme::from_memory(ui.ctx(), ui.style());
3243

33-
egui::Frame::new().fill(egui::Color32::RED).show(ui, |ui| {
34-
ui.label("Label with red background");
35-
});
44+
ui.hyperlink_to("The Rust Programming Language book", "https://doc.rust-lang.org/stable/book");
3645

3746
ui.horizontal(|ui| {
3847
ui.label("Write something: ");
@@ -52,8 +61,6 @@ pub fn central_panel_ui(
5261

5362
ui.separator();
5463

55-
56-
5764
ui.horizontal(|ui| {
5865

5966
ui.hyperlink_to("eframe source code.", "https://github.com/emilk/eframe_template");
@@ -66,6 +73,17 @@ pub fn central_panel_ui(
6673
if ui.button("Toggle Area 3").clicked() {
6774
*show_area3 = !*show_area3;
6875
}
76+
77+
ui.label("Language:");
78+
ui.add_enabled_ui(false, |ui|{
79+
ui.text_edit_singleline(language);
80+
});
81+
ui.collapsing("Theme", |ui| {
82+
ui.group(|ui| {
83+
theme.ui(ui);
84+
theme.clone().store_in_memory(ui.ctx());
85+
});
86+
});
6987
});
7088

7189
if *show_area1 {
@@ -77,25 +95,6 @@ pub fn central_panel_ui(
7795
.corner_radius(5),
7896
);
7997

80-
ui.horizontal(|ui| {
81-
ui.label("Language:");
82-
ui.text_edit_singleline(language);
83-
});
84-
ui.horizontal_wrapped(|ui| {
85-
ui.spacing_mut().item_spacing.x = 0.0;
86-
ui.label("Syntax highlighting powered by ");
87-
ui.hyperlink_to("syntect", "https://github.com/trishume/syntect");
88-
ui.label(".");
89-
90-
});
91-
92-
let mut theme = egui_extras::syntax_highlighting::CodeTheme::from_memory(ui.ctx(), ui.style());
93-
ui.collapsing("Theme", |ui| {
94-
ui.group(|ui| {
95-
theme.ui(ui);
96-
theme.clone().store_in_memory(ui.ctx());
97-
});
98-
});
9998
let mut layouter = |ui: &egui::Ui, text: &str, wrap_width: f32| {
10099
let mut layout_job = egui_extras::syntax_highlighting::highlight(
101100
ui.ctx(),
@@ -132,6 +131,12 @@ fn main() {
132131
.desired_width(f32::INFINITY)
133132
.layouter(&mut layouter),
134133
);
134+
ui.horizontal_wrapped(|ui| {
135+
ui.spacing_mut().item_spacing.x = 0.0;
136+
ui.label("Syntax highlighting powered by ");
137+
ui.hyperlink_to("syntect", "https://github.com/trishume/syntect");
138+
ui.label(".");
139+
});
135140
});
136141
}
137142

@@ -192,13 +197,16 @@ fn main() {
192197
.fit_to_exact_size(egui::vec2(50.0, 50.0))
193198
.corner_radius(5),
194199
);
195-
ui.label("Cool Floating Rustacean in an egui::Area. Areas are dragable, click the center and hold and drag area around to desired position. egui::Area itself does not directly control its size — it wraps content and takes the size of whatever UI elements are inside it.");
196200

197-
ui.add(
198-
egui::Image::new(egui::include_image!("../assets/Rust_logo_animated1.gif"))
199-
.fit_to_exact_size(egui::vec2(200.0, 200.0))
200-
.corner_radius(5),
201-
);
201+
ui.label("Cool Floating Rustacean in an egui::Area. Areas are dragable, Drag and Reposition an Area using the 'Rustferris' image in the top left of each area, or anywhere else on an Area it seems! just not on text. The position of an area persists from where it was last dragged and positioned. click the center and hold and drag area around to desired position. egui::Area itself does not directly control its size — it wraps content and takes the size of whatever UI elements are inside it.");
202+
egui::Frame::new().fill(egui::Color32::WHITE).show(ui, |ui| {
203+
ui.label(RichText::new("Label with white background, Rust hex color")
204+
.color(egui::Color32::from_rgb(183, 65, 14))
205+
.size(16.0)
206+
);
207+
});
208+
209+
202210
});
203211
}
204212

src/rustbook_code_blocks.rs

Lines changed: 180 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,44 @@ pub const VARIABLES_MUTABILITY_DATATYPES: &str = r#"
55
Variables, Mutability & DataTypes in Rust
66
77
// This code would not compile, because by default, variables are immutable, and as the RustBook states, 'When a variable is immutable, once a value is bound to a name, you cant change that value.'
8+
89
fn main() {
910
let x = 5;
1011
println!("The value of x is: {x}");
1112
x = 6;
1213
println!("The value of x is: {x}");
1314
}
1415
15-
// Good code that would compile bc value is declared mutable,
16+
// In the provided Rust example, a compilation error would occur because the code attempts to assign a new value to an 'immutable variable `x`', which is not allowed in Rust by default.
17+
18+
// Error Message
19+
20+
$ cargo run
21+
Compiling variables v0.1.0 (file:///projects/variables)
22+
error[E0384]: cannot assign twice to immutable variable `x`
23+
--> src/main.rs:4:5
24+
|
25+
2 | let x = 5;
26+
| - first assignment to `x`
27+
3 | println!("The value of x is: {x}");
28+
4 | x = 6;
29+
| ^^^^^ cannot assign twice to immutable variable
30+
|
31+
help: consider making this binding mutable
32+
|
33+
2 | let mut x = 5;
34+
| +++
35+
36+
For more information about this error, try `rustc --explain E0384`.
37+
error: could not compile `variables` (bin "variables") due to 1 previous error
38+
39+
// This happens because `x` was declared with `let x = 5;`, making it immutable, and then reassigned later with `x = 6;`.
40+
41+
// Solution:
42+
43+
To make the variable mutable, use `let mut x = 5;`.
44+
45+
// Good code that would compile bc 'x' is declared mutable,
1646
1747
fn main() {
1848
let mut x = 5;
@@ -21,6 +51,64 @@ fn main() {
2151
println!("The value of x is: {x}");
2252
}
2353
54+
$ cargo run
55+
Compiling variables v0.1.0 (file:///projects/variables)
56+
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.30s
57+
Running `target/debug/variables`
58+
The value of x is: 5
59+
The value of x is: 6
60+
61+
Why This Matters:
62+
63+
* Rust enforces `immutability by default` to prevent bugs that arise from unexpected value changes.
64+
* This makes code more predictable and easier to debug, especially in concurrent or complex systems.
65+
* If you need to change a variable, explicitly marking it as mutable (`mut`) signals that intent to other developers.
66+
67+
// Takeaway:
68+
// Compiler errors in Rust are designed to help you write **safe and correct code**. Even experienced developers encounter them — they’re part of the learning process and a core strength of Rust's safety guarantees.
69+
70+
**Constants**
71+
72+
// Constants are values bound to a name and are not allowed to change, similar to immutable variables. There are differences though between constants and variables, so lets take a look.
73+
74+
🧱 Key Characteristics
75+
76+
| Feature | Constants | Variables |
77+
| ------------------- | --------------------------------- | ------------------------ |
78+
| Mutability | Always immutable | Immutable by default |
79+
| Declaration Keyword | `const` | `let` or `let mut` |
80+
| Type Annotation | **Required** | Optional (type inferred) |
81+
| Scope | Any (including global scope) | Typically local scope |
82+
| Value Requirement | Must be a **constant expression** | Can be a runtime value |
83+
84+
Example:
85+
86+
const FERRIS_APPROVAL_RATING: u8 = 100;
87+
88+
**Shadowing**
89+
90+
// Shadowing allows you to redeclare a variable with the same name, effectively "shadowing" the previous one.
91+
// It can even change the **type** or **mutability**.
92+
93+
let z = 5;
94+
let z = z + 1; // shadows the previous z
95+
let z = z * 2; // shadows again
96+
println!("The value of z is: {}", z); // prints 12
97+
98+
let spaces = " ";
99+
let spaces = spaces.len(); // `spaces` now holds an integer
100+
101+
// Unlike `mut`, shadowing creates a **new variable**, which is useful for transformations.
102+
103+
Summary:
104+
105+
| Concept | Keyword | Mutable by Default | Can Change Type | Redeclaring Allowed |
106+
|-------------|-------------|--------------------|-----------------|-------------------------|
107+
| Variable | `let` | ❌ No | ❌ No | ✅ With shadowing |
108+
| Mutable Var | `let mut` | ✅ Yes | ❌ No | ❌ |
109+
| Constant | `const` | ❌ No | ❌ No | ❌ |
110+
| Shadowing | `let` again | ✅ Yes | ✅ Yes | ✅ (via new declaration) |
111+
24112
___________________________________________________________________________________
25113
Data Types
26114
@@ -117,5 +205,95 @@ pub const FUNCTIONS: &str = r#"
117205
118206
Functions are used prevalently in Rust.
119207
120-
There is one of the most important funtions, the 'main' function.
208+
There is one of the most important funtions, the 'main' function, the entry point of many programs. In RUST we use the fn keyword to allow us to declare new functions.
209+
210+
For function and variable names, RUST uses snake case, where all letters are lowercase and underscores seperate words. example_snake_case
211+
212+
// ================================================
213+
// 1. Function Basics
214+
// ================================================
215+
216+
// Functions are declared using the `fn` keyword.
217+
// The name follows snake_case convention.
218+
// Function definitions go before they are called.
219+
220+
fn main() {
221+
println!("Welcome to the Rust functions demo!");
222+
223+
// Calling a basic function
224+
basic_function();
225+
226+
// Calling a function with parameters
227+
print_measurement(5, 'h');
228+
229+
// Calling a function that returns a value
230+
let result = plus_one(5);
231+
println!("5 + 1 = {}", result);
232+
}
233+
234+
// ================================================
235+
// 2. Defining and Calling Functions
236+
// ================================================
237+
fn basic_function() {
238+
// This function takes no arguments and returns nothing
239+
println!("This is a basic function call.");
240+
}
241+
242+
// ================================================
243+
// 3. Function Parameters
244+
// ================================================
245+
// Parameters are variables that are part of a function's signature
246+
247+
fn print_measurement(value: i32, unit_label: char) {
248+
// Parameters must have types explicitly declared
249+
println!("The measurement is: {}{}", value, unit_label);
250+
}
251+
252+
// ================================================
253+
// 4. Function Return Values
254+
// ================================================
255+
// Functions can return values with the `->` syntax
256+
257+
fn plus_one(x: i32) -> i32 {
258+
// Implicit return: no semicolon means the value is returned
259+
x + 1
260+
// If you put a semicolon here, it becomes a statement, not an expression, and the function will return `()`
261+
}
262+
263+
// ================================================
264+
// 5. Statements vs Expressions
265+
// ================================================
266+
// - Statements: instructions that perform actions and do not return a value
267+
// - Expressions: evaluate to a value
268+
269+
// For example:
270+
fn _statement_vs_expression() {
271+
let y = {
272+
let x = 3;
273+
x + 1 // This is an expression, the value of this block is 4
274+
};
275+
println!("y = {}", y);
276+
}
277+
278+
// ================================================
279+
// 6. Practice: Writing Your Own Functions
280+
// ================================================
281+
// Challenge yourself to:
282+
// - Write a function that takes 3 parameters
283+
// - Return a computed result
284+
285+
// Example:
286+
fn multiply_add(a: i32, b: i32, c: i32) -> i32 {
287+
(a * b) + c
288+
}
289+
290+
// ================================================
291+
// 7. Recap
292+
// ================================================
293+
// - `fn` to define functions
294+
// - Parameters require explicit types
295+
// - Use `->` to specify a return type
296+
// - Expression without semicolon for return
297+
// - Function calls must match parameter types
298+
121299
"#;

0 commit comments

Comments
 (0)