avatarJohn Philip

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2388

Abstract

<span class="hljs-comment">// I AM NOT DONE</span>

<span class="hljs-keyword">use</span> std::<span class="hljs-type">f32</span>;

<span class="hljs-keyword">fn</span> <span class="hljs-title function_">main</span>() { <span class="hljs-keyword">let</span> <span class="hljs-variable">pi</span> = <span class="hljs-number">3.14f32</span>; <span class="hljs-keyword">let</span> <span class="hljs-variable">radius</span> = <span class="hljs-number">5.00f32</span>;

<span class="hljs-keyword">let</span> <span class="hljs-variable">area</span> = pi * <span class="hljs-type">f32</span>::<span class="hljs-title function_ invoke__">powi</span>(radius, <span class="hljs-number">2</span>);

<span class="hljs-built_in">println!</span>(
    <span class="hljs-string">"The area of a circle with radius {:.2} is {:.5}!"</span>,
    radius, area
)

}</pre></div><h2 id="b985">Explanation:</h2><p id="719f">Clippy throws an error that wants us to use the constant pi.</p><div id="c8c9"><pre>error: approximate value of <span class="hljs-type">f32</span>::consts::PI found -<span class="hljs-punctuation">-></span> src/lib.rs:<span class="hljs-number">17</span>:<span class="hljs-number">14</span> | <span class="hljs-number">17</span> | <span class="hljs-keyword">let</span> <span class="hljs-variable">pi</span> = <span class="hljs-number">3.14f32</span>; | ^^^^^^^ | = help: consider using the constant directly = help: <span class="hljs-keyword">for</span> <span class="hljs-variable">further</span> <span class="hljs-keyword">in</span>formation visit https:<span class="hljs-comment">//rust-lang.github.io/rust-clippy/master/index.html#approx_constant</span> = note: <span class="hljs-meta">#[deny(clippy::approx_constant)]</span> on by default</pre></div><p id="0da3">Clippy linter provides suggestions for improving the code quality and recommends not using approximate constants like 3.14 directly.</p><p id="d31f">To fix this clippy warning, we can use the constant provided by the standard library for π (pi) directly.</p><h2 id="6709">Solution:</h2><div id="d54d"><pre><span class="hljs-keyword">use</span> std::<span class="hljs-type">f32</span>::consts::PI;

<span class="hljs-keyword">fn</span> <span class="hljs-title function_">main</span>() { <span class="hljs-keyword">let</span> <span class="hljs-var

Options

iable">radius</span> = <span class="hljs-number">5.00f32</span>;

<span class="hljs-keyword">let</span> <span class="hljs-variable">area</span> = PI * <span class="hljs-type">f32</span>::<span class="hljs-title function_ invoke__">powi</span>(radius, <span class="hljs-number">2</span>);

<span class="hljs-built_in">println!</span>(
    <span class="hljs-string">"The area of a circle with radius {:.2} is {:.5}!"</span>,
    radius, area
);

}</pre></div><h2 id="1fae">Resources</h2><ul><li><a href="https://github.com/rust-lang/rust-clippy"><b><i>Clippy</i></b></a></li><li><a href="https://doc.rust-lang.org/rust-by-example/custom_types/constants.html"><b><i>Constants</i></b></a></li></ul><h2 id="99b5">Before you go</h2><p id="98fc">Thank you for taking the time to read through this challenge. We invite you to share your knowledge of Rust as well. If you found this article valuable, please don’t hesitate to share it with others. Don’t forget to follow the publication and give the article some claps 👏.</p><p id="e9c0">Thank you, and we look forward to seeing you for the next challenges!</p><h2 id="86b2">More reads</h2><div id="4593" class="link-block"> <a href="https://readmedium.com/write-for-us-at-rustancean-121ed52eae"> <div> <div> <h2>Write for Us at Rustaceans</h2> <div><h3>Join Our Rustaceans Community: Share Your Expertise and Inspire Learning</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*c-hqTpwb0rml3dxS)"></div> </div> </div> </a> </div><div id="9fcd" class="link-block"> <a href="https://readmedium.com/rustlings-macros4-rs-issue79-macros-in-rust-117ea67a22f1"> <div> <div> <h2>Rustlings: macros4.rs #Issue79 — Macros in Rust</h2> <div><h3>Rustlings Challenge: macros4.rs Solution Walkthrough</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*O91T72-Ou-5iT10T96CgGQ.png)"></div> </div> </div> </a> </div></article></body>

Rustlings: clippy1.rs #Issue84— Clippy in Rust

Rustlings Challenge: clippy1.rs Solution Walkthrough

Image by Brave

This is the Eighty-fourth (84th) issue of the Rustlings series. In this issue, we provide solutions to Rustlings exercises along with detailed explanations. In this issue we will solve the challenge on clippy1.rs.

Previous challenge #Issue 83

Clippy is a popular and widely used tool in the Rust ecosystem. It is a collection of linters that are designed to analyze your Rust code for potential issues, bugs, and style violations.

Clippy provides warnings and suggestions to help us write safer, more idiomatic, and high-quality Rust code.

Challenge:

// clippy1.rs
//
// The Clippy tool is a collection of lints to analyze your code so you can
// catch common mistakes and improve your Rust code.
//
// For these exercises the code will fail to compile when there are clippy
// warnings check clippy's suggestions from the output to solve the exercise.
//
// Execute `rustlings hint clippy1` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

use std::f32;

fn main() {
    let pi = 3.14f32;
    let radius = 5.00f32;

    let area = pi * f32::powi(radius, 2);

    println!(
        "The area of a circle with radius {:.2} is {:.5}!",
        radius, area
    )
}

Explanation:

Clippy throws an error that wants us to use the constant pi.

error: approximate value of `f32::consts::PI` found
  --> src/lib.rs:17:14
   |
17 |     let pi = 3.14f32;
   |              ^^^^^^^
   |
   = help: consider using the constant directly
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#approx_constant
   = note: `#[deny(clippy::approx_constant)]` on by default

Clippy linter provides suggestions for improving the code quality and recommends not using approximate constants like 3.14 directly.

To fix this clippy warning, we can use the constant provided by the standard library for π (pi) directly.

Solution:

use std::f32::consts::PI;

fn main() {
    let radius = 5.00f32;

    let area = PI * f32::powi(radius, 2);

    println!(
        "The area of a circle with radius {:.2} is {:.5}!",
        radius, area
    );
}

Resources

Before you go

Thank you for taking the time to read through this challenge. We invite you to share your knowledge of Rust as well. If you found this article valuable, please don’t hesitate to share it with others. Don’t forget to follow the publication and give the article some claps 👏.

Thank you, and we look forward to seeing you for the next challenges!

More reads

Rust
Rustlang
Rust Programming Language
Rustling
Programming
Recommended from ReadMedium