Skip to content

Commit

Permalink
style: simplify string formatting for readability (#15033)
Browse files Browse the repository at this point in the history
# Objective

The goal of this change is to improve code readability and
maintainability.
  • Loading branch information
hamirmahal authored Sep 3, 2024
1 parent 4ac2a63 commit ec728c3
Show file tree
Hide file tree
Showing 12 changed files with 27 additions and 37 deletions.
2 changes: 1 addition & 1 deletion examples/3d/anti_aliasing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ fn setup(
/// Writes a simple menu item that can be on or off.
fn draw_selectable_menu_item(ui: &mut String, label: &str, shortcut: char, enabled: bool) {
let star = if enabled { "*" } else { "" };
let _ = writeln!(*ui, "({}) {}{}{}", shortcut, star, label, star);
let _ = writeln!(*ui, "({shortcut}) {star}{label}{star}");
}

/// Creates a colorful test pattern
Expand Down
6 changes: 3 additions & 3 deletions examples/3d/color_grading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,9 +459,9 @@ impl Display for SelectedSectionColorGradingOption {
impl Display for SelectedColorGradingOption {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
SelectedColorGradingOption::Global(option) => write!(f, "\"{}\"", option),
SelectedColorGradingOption::Global(option) => write!(f, "\"{option}\""),
SelectedColorGradingOption::Section(section, option) => {
write!(f, "\"{}\" for \"{}\"", option, section)
write!(f, "\"{option}\" for \"{section}\"")
}
}
}
Expand Down Expand Up @@ -633,7 +633,7 @@ fn update_ui_state(

/// Creates the help text at the top left of the window.
fn create_help_text(currently_selected_option: &SelectedColorGradingOption) -> String {
format!("Press Left/Right to adjust {}", currently_selected_option)
format!("Press Left/Right to adjust {currently_selected_option}")
}

/// Processes keyboard input to change the value of the currently-selected color
Expand Down
11 changes: 5 additions & 6 deletions examples/3d/irradiance_volumes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,12 +351,11 @@ impl AppStatus {

Text::from_section(
format!(
"{}\n{}\n{}\n{}\n{}",
CLICK_TO_MOVE_HELP_TEXT,
voxels_help_text,
irradiance_volume_help_text,
rotation_help_text,
switch_mesh_help_text
"{CLICK_TO_MOVE_HELP_TEXT}
{voxels_help_text}
{irradiance_volume_help_text}
{rotation_help_text}
{switch_mesh_help_text}"
),
TextStyle::default(),
)
Expand Down
2 changes: 1 addition & 1 deletion examples/app/headless_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ fn update(
// Finally saving image to file, this heavy blocking operation is kept here
// for example simplicity, but in real app you should move it to a separate task
if let Err(e) = img.save(image_path) {
panic!("Failed to save image: {}", e);
panic!("Failed to save image: {e}");
};
}
if scene_controller.single_image {
Expand Down
10 changes: 2 additions & 8 deletions examples/ecs/component_hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,7 @@ fn setup(world: &mut World) {
.on_add(|mut world, entity, component_id| {
// You can access component data from within the hook
let value = world.get::<MyComponent>(entity).unwrap().0;
println!(
"Component: {:?} added to: {:?} with value {:?}",
component_id, entity, value
);
println!("Component: {component_id:?} added to: {entity:?} with value {value:?}");
// Or access resources
world
.resource_mut::<MyComponentIndex>()
Expand All @@ -96,10 +93,7 @@ fn setup(world: &mut World) {
// since it runs before the component is removed you can still access the component data
.on_remove(|mut world, entity, component_id| {
let value = world.get::<MyComponent>(entity).unwrap().0;
println!(
"Component: {:?} removed from: {:?} with value {:?}",
component_id, entity, value
);
println!("Component: {component_id:?} removed from: {entity:?} with value {value:?}");
// You can also issue commands through `.commands()`
world.commands().entity(entity).despawn();
});
Expand Down
14 changes: 7 additions & 7 deletions examples/ecs/dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ fn main() {
let mut component_names = HashMap::<String, ComponentId>::new();
let mut component_info = HashMap::<ComponentId, ComponentInfo>::new();

println!("{}", PROMPT);
println!("{PROMPT}");
loop {
print!("\n> ");
let _ = std::io::stdout().flush();
Expand All @@ -64,10 +64,10 @@ fn main() {

let Some((first, rest)) = line.trim().split_once(|c: char| c.is_whitespace()) else {
match &line.chars().next() {
Some('c') => println!("{}", COMPONENT_PROMPT),
Some('s') => println!("{}", ENTITY_PROMPT),
Some('q') => println!("{}", QUERY_PROMPT),
_ => println!("{}", PROMPT),
Some('c') => println!("{COMPONENT_PROMPT}"),
Some('s') => println!("{ENTITY_PROMPT}"),
Some('q') => println!("{QUERY_PROMPT}"),
_ => println!("{PROMPT}"),
}
continue;
};
Expand Down Expand Up @@ -112,7 +112,7 @@ fn main() {

// Get the id for the component with the given name
let Some(&id) = component_names.get(name) else {
println!("Component {} does not exist", name);
println!("Component {name} does not exist");
return;
};

Expand Down Expand Up @@ -245,7 +245,7 @@ fn parse_term<Q: QueryData>(
};

if !matched {
println!("Unable to find component: {}", str);
println!("Unable to find component: {str}");
}
}

Expand Down
2 changes: 1 addition & 1 deletion examples/ecs/send_and_receive_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ fn send_events(mut events: EventWriter<DebugEvent>, frame_count: Res<FrameCount>
/// Note that some events will be printed twice, because they were sent twice.
fn debug_events(mut events: EventReader<DebugEvent>) {
for event in events.read() {
println!("{:?}", event);
println!("{event:?}");
}
}

Expand Down
2 changes: 1 addition & 1 deletion examples/games/stepping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ fn build_ui(
for label in schedule_order {
let schedule = schedules.get(*label).unwrap();
text_sections.push(TextSection::new(
format!("{:?}\n", label),
format!("{label:?}\n"),
TextStyle {
font: asset_server.load(FONT_BOLD),
color: FONT_COLOR,
Expand Down
4 changes: 2 additions & 2 deletions examples/math/cubic_splines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ fn setup(mut commands: Commands) {
R: Remove the last control point\n\
S: Cycle the spline construction being used\n\
C: Toggle cyclic curve construction";
let spline_mode_text = format!("Spline: {}", spline_mode);
let cycling_mode_text = format!("{}", cycling_mode);
let spline_mode_text = format!("Spline: {spline_mode}");
let cycling_mode_text = format!("{cycling_mode}");
let style = TextStyle::default();

commands
Expand Down
3 changes: 1 addition & 2 deletions examples/stress_tests/many_cubes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ impl FromStr for Layout {
"cube" => Ok(Self::Cube),
"sphere" => Ok(Self::Sphere),
_ => Err(format!(
"Unknown layout value: '{}', valid options: 'cube', 'sphere'",
s
"Unknown layout value: '{s}', valid options: 'cube', 'sphere'"
)),
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/time/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fn runner(mut app: App) -> AppExit {
let stdin = io::stdin();
for line in stdin.lock().lines() {
if let Err(err) = line {
println!("read err: {:#}", err);
println!("read err: {err:#}");
break;
}
match line.unwrap().as_str() {
Expand Down
6 changes: 2 additions & 4 deletions tests/ecs/ambiguity_detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,15 @@ fn main() {
assert_eq!(
main_app_ambiguities.total(),
0,
"Main app has unexpected ambiguities among the following schedules: \n{:#?}.",
main_app_ambiguities,
"Main app has unexpected ambiguities among the following schedules: \n{main_app_ambiguities:#?}.",
);

// RenderApp is not checked here, because it is not within the App at this point.
let render_extract_ambiguities = count_ambiguities(app.sub_app(RenderExtractApp));
assert_eq!(
render_extract_ambiguities.total(),
0,
"RenderExtract app has unexpected ambiguities among the following schedules: \n{:#?}",
render_extract_ambiguities,
"RenderExtract app has unexpected ambiguities among the following schedules: \n{render_extract_ambiguities:#?}",
);
}

Expand Down

0 comments on commit ec728c3

Please sign in to comment.