Coder Social home page Coder Social logo

nayantha / rectangles_with_rust Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 12 KB

This code base developed on the concept of struct in RUST language and use rectangle as the primary object of struct example. Contain struct define, implementing methods, printing struct structures. Tutorial is from: “No.Starch.Press.The.Rust.Programming.Language”

Rust 100.00%
rust rust-lang struct

rectangles_with_rust's Introduction

title slug excerpt date author
Struct in Rust
struct-in-rust
Explain the struct in Rust. Rectangle is used to demonstrate the behaviour of the struct.
09.10.23
Nayantha Yasiru

Struct in Rust

Rectangle is used to Demonstrate

Define Struct

#[derive(Debug)] can be used to easily print struct on the output / CMD.
{:?} - list all the values in single line
{:#?} - format the struct (similar to json format)

struct Rectangle {
    width: u32, // fields
    height: u32,
}

Implement Methods


impl Rectangle {
    fn area(&self) -> u32 { // self is calling itself
        self.width * self.height
    }
    fn can_hold(&self, rect: &Rectangle) -> bool {
        self.area() >= rect.area() && (
            (self.width >= rect.width && self.height >= rect.height) ||
                (self.width >= rect.height && self.height >= rect.width)
        )
    }
    fn make_square(size: u32) -> Rectangle { // associated method - not calling self method
        Rectangle { width: size, height: size }
    }
}

Struct Tips


Struct Construct
Normal

fn build_user(email: String, username: String) -> User {
    User {
        email: email,
        username: username,
        active: true,
        sign_in_count: 1,
    }
}


Simple & Fast Way

fn build_user(email: String, username: String) -> User {
    User {
        email,
        username,
        active: true,
        sign_in_count: 1,
    }
}

Constuct from another struct - Normal
let user2 = User {
    email: String::from("[email protected]"),
    username: String::from("anotherusername567"),
    active: user1.active,
    sign_in_count: user1.sign_in_count,
};


Simple & Fast Way

let user2 = User {
    email: String::from("[email protected]"),
    username: String::from("anotherusername567"),
    ..user1
};

rectangles_with_rust's People

Contributors

nayantha avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.