avatarAnkit Tanna

Summary

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

1927

Abstract

ong with else. We also need to use <code>{}</code> always with conditionals.</p><p id="1593">Return type cannot be inferred and we need to be quite explicit about it in Rust.</p><p id="5b99">Below snippet shows usage of typecasting from various types. You’ll see the general pattern of typecasting rules being applied here. We use higher memory types so as we do not lose information while typecasting.</p><div id="720f"><pre><span class="hljs-keyword">fn</span> <span class="hljs-title function_">main</span>() { <span class="hljs-keyword">let</span> <span class="hljs-variable">city_name</span>: &<span class="hljs-type">str</span> = <span class="hljs-string">"Rustville"</span>;

<span class="hljs-built_in">println!</span>(<span class="hljs-string">"The city of {}:\n"</span>, city_name);

<span class="hljs-title function_ invoke__">print_population</span>(<span class="hljs-number">1_324_578</span>, <span class="hljs-number">114_293</span>, <span class="hljs-number">108_097</span>);

}

<span class="hljs-keyword">fn</span> <span class="hljs-title function_">print_population</span>(adults: <span class="hljs-type">u64</span>, kids: <span class="hljs-type">u32</span>, buildings: <span class="hljs-type">u32</span>) { <span class="hljs-keyword">let</span> <span class="hljs-variable">population</span>: <span class="hljs-type">i32</span> = adults <span class="hljs-keyword">as</span> <span class="hljs-type">i32</span> + kids <span class="hljs-keyword">as</span> <span class="hljs-type">i32</span>;

<span class="hljs-keyword">let</span> <span class="hljs-variable">buildings_per_person</span>: <span class="hljs-type">f64</span> = buildings <span class="hljs-keyword">as</span> <span class="hljs-type">f64</span> / population <span class="hljs-keyword">as</span> <span class="hljs-type">f64</span>;

<span class="hljs-built_in">println!</span>(<span class="hljs-string">"    Population: {}"<

Options

/span>, population); <span class="hljs-built_in">println!</span>(<span class="hljs-string">" Adults: {}"</span>, adults); <span class="hljs-built_in">println!</span>(<span class="hljs-string">" Kids: {}"</span>, kids); <span class="hljs-built_in">println!</span>(<span class="hljs-string">" Buildings: {}"</span>, buildings); <span class="hljs-built_in">println!</span>(<span class="hljs-string">" Buildings per person: {}\n"</span>, buildings_per_person);

<span class="hljs-keyword">if</span> buildings_per_person &gt;= <span class="hljs-number">1.0</span> {
    <span class="hljs-built_in">println!</span>(<span class="hljs-string">"Everyone can have their own building!"</span>);
} <span class="hljs-keyword">else</span> {
    <span class="hljs-built_in">println!</span>(<span class="hljs-string">"Buildings must be shared!"</span>);
}

}</pre></div><p id="8c69">That’s it for primitives in The Rust Programming Language. We’ll discuss collections in the next section.</p><p id="8121">You can subscribe to my newsletter about <b>The Rust Programming Language <a href="https://tinyletter.com/ankittanna"></a></b><a href="https://tinyletter.com/ankittanna">here</a>. You can read about all the articles in this series <a href="https://readmedium.com/the-rust-programming-language-4b22bc717ecc">here</a>.</p><h2 id="2fb8">Rustaceans 🚀</h2><p id="d523">Thank you for being a part of the Rustaceans community! Before you go:</p><ul><li>Show your appreciation with a clap and follow the publication</li><li>Discover how you can contribute your own insights to <a href="https://readmedium.com/rustaceans-submissions-are-now-open-924e67cc595d"><b><i>Rustaceans</i></b></a>.</li><li>Connect with us: <a href="https://twitter.com/rustaceans_rs"><b><i>X</i></b></a> | <a href="https://substack.com/@weeklyrust"><b><i>Weekly Rust Newsletter</i></b></a></li></ul></article></body>

The Rust Programming Language

The Rust Programming Language — Primitives — Summary

We’ve seen string, floats, integers and booleans and how they work together. We’ve also seen statements and expressions and subtle rules that Rust Compiler puts in place.

Primitives in Rust

We saw macros like println!(), format!() and panic!() which allow you to log the strings, generate strings and throw an error and stop the program from execution respectively.

We also saw how string interpolation works using {} in Rust.

We came across floats in Rust and mutability concept using mut keyword. By default a float number in Rust would be of type f64.

We came across integers in Rust and by default, Rust would infer an integer to be of type i32.

We also saw typecasting using as keyword and you can typecast from one type to another (generally we do from lower memory type to higher memory type typecasting). When we typecast float to signed or unsigned integer it drops the decimals. So when we typecast 1.99 as u32 the output would be 1.

We also came across booleans where we learnt that Rust compiler recognises only true and false for conditionals and throws a compiler error for truthy and falsy values. There is no ternary operator in Rust. We need to use if statement for it along with else. We also need to use {} always with conditionals.

Return type cannot be inferred and we need to be quite explicit about it in Rust.

Below snippet shows usage of typecasting from various types. You’ll see the general pattern of typecasting rules being applied here. We use higher memory types so as we do not lose information while typecasting.

fn main() {
    let city_name: &str = "Rustville";

    println!("The city of {}:\n", city_name);

    print_population(1_324_578, 114_293, 108_097);
}

fn print_population(adults: u64, kids: u32, buildings: u32) {
    let population: i32 = adults as i32 + kids as i32;

    let buildings_per_person: f64 = buildings as f64 / population as f64;

    println!("    Population: {}", population);
    println!("        Adults: {}", adults);
    println!("        Kids: {}", kids);
    println!("    Buildings: {}", buildings);
    println!("    Buildings per person: {}\n", buildings_per_person);

    if buildings_per_person >= 1.0 {
        println!("Everyone can have their own building!");
    } else {
        println!("Buildings must be shared!");
    }
}

That’s it for primitives in The Rust Programming Language. We’ll discuss collections in the next section.

You can subscribe to my newsletter about The Rust Programming Language here. You can read about all the articles in this series here.

Rustaceans 🚀

Thank you for being a part of the Rustaceans community! Before you go:

  • Show your appreciation with a clap and follow the publication
  • Discover how you can contribute your own insights to Rustaceans.
  • Connect with us: X | Weekly Rust Newsletter
Rust
Rust Programming Language
Rustlang
Programming
Webassembly
Recommended from ReadMedium