Rust  Language

Rust Language

1640143031

Structs - The Rust Programming Language

Rust For Beginners Tutorial - Structs

In this video we're taking. a closer look at structs in Rust! Learn about classic structs, tuple structs and unit structs as well as how to implement methods on structs!

Exercise solutions: https://github.com/PascalPrecht/rustlings/commits/solutions 

---
0:00 Intro
0:16 Exercise 1
6:13 Exercise 2
8:40 Exercise 3
13:59 Outro


Struct

A type that is composed of other types.

Structs in Rust come in three flavors: Structs with named fields, tuple structs, and unit structs.

struct Regular {
    field1: f32,
    field2: String,
    pub field3: bool
}

struct Tuple(u32, String);

struct Unit;

Regular structs are the most commonly used. Each field defined within them has a name and a type, and once defined can be accessed using example_struct.field syntax. The fields of a struct share its mutability, so foo.bar = 2; would only be valid if foo was mutable. Adding pub to a field makes it visible to code in other modules, as well as allowing it to be directly accessed and modified.

Tuple structs are similar to regular structs, but its fields have no names. They are used like tuples, with deconstruction possible via let TupleStruct(x, y) = foo; syntax. For accessing individual variables, the same syntax is used as with regular tuples, namely foo.0, foo.1, etc, starting at zero.

Unit structs are most commonly used as marker. They have a size of zero bytes, but unlike empty enums they can be instantiated, making them isomorphic to the unit type (). Unit structs are useful when you need to implement a trait on something, but don’t need to store any data inside it.

Instantiation

Structs can be instantiated in different ways, all of which can be mixed and matched as needed. The most common way to make a new struct is via a constructor method such as new(), but when that isn’t available (or you’re writing the constructor itself), struct literal syntax is used:

let example = Foo {
    field1: 42.0,
    field2: "blah".to_string(),
    etc: true,
};

It’s only possible to directly instantiate a struct using struct literal syntax when all of its fields are visible to you.

There are a handful of shortcuts provided to make writing constructors more convenient, most common of which is the Field Init shorthand. When there is a variable and a field of the same name, the assignment can be simplified from field: field into simply field. The following example of a hypothetical constructor demonstrates this:

struct User {
    name: String,
    admin: bool,
}

impl User {
    pub fn new(name: String) -> Self {
        Self {
            name,
            admin: false,
        }
    }
}

Another shortcut for struct instantiation is available, used when you need to make a new struct that has the same values as most of a previous struct of the same type, called struct update syntax:

let updated_thing = Foo {
    field1: "a new value".to_string(),
    ..thing
};

Tuple structs are instantiated in the same way as tuples themselves, except with the struct’s name as a prefix: Foo(123, false, 0.1).

Empty structs are instantiated with just their name, and don’t need anything else. let thing = EmptyStruct;

Style conventions

Structs are always written in CamelCase, with few exceptions. While the trailing comma on a struct’s list of fields can be omitted, it’s usually kept for convenience in adding and removing fields down the line.

For more information on structs, take a look at the Rust Book or the Reference.


Structures

There are three types of structures ("structs") that can be created using the struct keyword:

  • Tuple structs, which are, basically, named tuples.
  • The classic C structs
  • Unit structs, which are field-less, are useful for generics.
#[derive(Debug)]
struct Person {
    name: String,
    age: u8,
}

// A unit struct
struct Unit;

// A tuple struct
struct Pair(i32, f32);

// A struct with two fields
struct Point {
    x: f32,
    y: f32,
}

// Structs can be reused as fields of another struct
#[allow(dead_code)]
struct Rectangle {
    // A rectangle can be specified by where the top left and bottom right
    // corners are in space.
    top_left: Point,
    bottom_right: Point,
}

fn main() {
    // Create struct with field init shorthand
    let name = String::from("Peter");
    let age = 27;
    let peter = Person { name, age };

    // Print debug struct
    println!("{:?}", peter);


    // Instantiate a `Point`
    let point: Point = Point { x: 10.3, y: 0.4 };

    // Access the fields of the point
    println!("point coordinates: ({}, {})", point.x, point.y);

    // Make a new point by using struct update syntax to use the fields of our
    // other one
    let bottom_right = Point { x: 5.2, ..point };

    // `bottom_right.y` will be the same as `point.y` because we used that field
    // from `point`
    println!("second point: ({}, {})", bottom_right.x, bottom_right.y);

    // Destructure the point using a `let` binding
    let Point { x: left_edge, y: top_edge } = point;

    let _rectangle = Rectangle {
        // struct instantiation is an expression too
        top_left: Point { x: left_edge, y: top_edge },
        bottom_right: bottom_right,
    };

    // Instantiate a unit struct
    let _unit = Unit;

    // Instantiate a tuple struct
    let pair = Pair(1, 0.1);

    // Access the fields of a tuple struct
    println!("pair contains {:?} and {:?}", pair.0, pair.1);

    // Destructure a tuple struct
    let Pair(integer, decimal) = pair;

    println!("pair contains {:?} and {:?}", integer, decimal);
}

Activity

  1. Add a function rect_area which calculates the area of a Rectangle (try using nested destructuring).
  2. Add a function square which takes a Point and a f32 as arguments, and returns a Rectangle with its lower left corner on the point, and a width and height corresponding to the f32.

#rust #programming #developer 

What is GEEK

Buddha Community

Structs - The Rust Programming Language

RUST on programming language

The title is a bit confusing to understand the context of the content. In this blog, I am going to run around and see the different aspects of programming language rust. And talk about the concepts that it introduces that are useful for various aspects of programming.

What is Rust?

Simply putting it is a statically as well as strongly typed programming language.

Let me explain:

_statically typed _indicates that all the datatypes that are expressed in the code are known at compile time and memory allocation is done properly.

Image for post

Then what is 👆 that? Let’s just say rust knows what you want to say.

But this doesn’t mean you could declare variables for a complex data type and expect rust to understand. Here comes the next point I mentioned above.

_strongly typed _indicates that the types are designed to make it harder to write syntatically incorrect code.

If you were to do even a little mistake with the syntax or definition of variables then the errors are caught at compile time. Not just the syntax errors but there are various tests build in the compiler to check for unused variablesdead code(Code that will never run), infinite loops as well as the lifetime of variables.

#security #programming #programming-languages #rust

Cayla  Erdman

Cayla Erdman

1594369800

Introduction to Structured Query Language SQL pdf

SQL stands for Structured Query Language. SQL is a scripting language expected to store, control, and inquiry information put away in social databases. The main manifestation of SQL showed up in 1974, when a gathering in IBM built up the principal model of a social database. The primary business social database was discharged by Relational Software later turning out to be Oracle.

Models for SQL exist. In any case, the SQL that can be utilized on every last one of the major RDBMS today is in various flavors. This is because of two reasons:

1. The SQL order standard is genuinely intricate, and it isn’t handy to actualize the whole standard.

2. Every database seller needs an approach to separate its item from others.

Right now, contrasts are noted where fitting.

#programming books #beginning sql pdf #commands sql #download free sql full book pdf #introduction to sql pdf #introduction to sql ppt #introduction to sql #practical sql pdf #sql commands pdf with examples free download #sql commands #sql free bool download #sql guide #sql language #sql pdf #sql ppt #sql programming language #sql tutorial for beginners #sql tutorial pdf #sql #structured query language pdf #structured query language ppt #structured query language

Biju Augustian

Biju Augustian

1574340419

Guide to Python Programming Language

Description
The course will lead you from beginning level to advance in Python Programming Language. You do not need any prior knowledge on Python or any programming language or even programming to join the course and become an expert on the topic.

The course is begin continuously developing by adding lectures regularly.

Please see the Promo and free sample video to get to know more.

Hope you will enjoy it.

Basic knowledge
An Enthusiast Mind
A Computer
Basic Knowledge To Use Computer
Internet Connection
What will you learn
Will Be Expert On Python Programming Language
Build Application On Python Programming Language

#uide to Python #Guide to Python Programming #Guide to Python Programming Language #Python Programming #Python Programming Language

Alayna  Rippin

Alayna Rippin

1600898400

OS in Rust: An executable that runs on bare metal

This is the very first blog of the series that pertains to create a basic Operating System using Rust Programming Language.

The aim of this series is to learn and understand the basics of Operating System. Through this series, you will get some ideas about the internal components of Operating System and how they interact with each other.

In this article, we will create a freestanding binary (an executable) that has the capability to run on bare metal. To create that executable we need to follow certain steps:

Steps to create a bare-metal executable:

  • Disable standard library
  • Define custom panic handler
  • Provide language items
  • Provide entry point
  • Build executable

#functional programming #rust #rust programming language #system programming

Iliana  Welch

Iliana Welch

1590893280

This Rust programming language

This Rust programming language tutorial series is aimed at easing your training step by step. Rust is a systems level language aimed at speed and safety and can be run cross-platform, including embedded systems and even the browser with WebAssembly (WASM)! I use the VS Code development environment to help you learn the core topics. Please join me on this journey with this fantastic new programming language.

Subscribe to the channel https://www.youtube.com/watch?v=afFehipXJFA

#rust #programming #language