Directory Image
This website uses cookies to improve user experience. By using our website you consent to all cookies in accordance with our Privacy Policy.

Variable Scope in Rust!!

Author: Mansoor Ahmed
by Mansoor Ahmed
Posted: Nov 01, 2020
  • Rust is a programming language concentrated on performance and safety.

  • It does not use garbage collection unlike other programming languages.

  • Rust provides deterministic management of resources, with very low overhead.

  • It is planned to be a language for highly coexisting and highly safe systems.

  • In Rust each variable has a scope that starts where the variable in initialized.

  • We may have variable from the larger scope be visible to the scopes that are within that larger scope where the variable in initialized.

  • We can assign variable with the result of a statement in a single assignment

  • So assigning the variable from the scope of an if statement is perfectly valid.

Variable Scope

  • A scope is the place where a block expression stores its variables.

  • Scopes are not directly represented in the source code, but a scope begins when a block expression begins, with a `{` symbol, and ends when the block expression ends, with `}` (or when a `return` statement is run before the block reaches its end).

  • The scope is the chunk of memory where the block’s variables are stored.

  • A scope is the range within a program for which an item is valid.

  • When variable comes into scope, it is valid. It remains valid until it goes out of scope.

    Example

    fn main()

    {// s is not valid here, it’s not yet declared

    let s = "hello";

    // s is valid from this point forward

    // do stuff with s

    println!("{}", s);

    }// this scope is now over, and s is no longer valid

  • The variable "s" refers to a string literal, where the value of the string is

  • hardcoded into the text of our program. The variable is valid from the point at which it’s declared until the end of the current scope.

  • When "s" comes into scope, it is valid.

  • It remains valid until it goes out of scope

  • The relationship between scopes and when variables are valid is similar to that in other programming languages.

  • Now we’ll build on top of this understanding by introducing the String type.

The String Type

  • Rust has a second string type, String.

  • This type is allocated on the heap and is able to store an amount of text that is unknown to us at compile time.

  • We can create a String from a string literal using the from function, like:

    let s = String::from("hello");

  • The double colon (::) is an operator that allows us to namespace this particular from function under the String type rather than using some sort of name like string_from.

  • This kind of string can be mutated:

    let mut s = String::from("hello");

    s.push_str(", world!"); // appends a literal to a String

    println!("{}", s); // This will print `hello, world! `

  • The difference between String Literal and String type is how these two types deal with memory.

String: Memory and Allocation

  • The memory must be requested from the Operating System at runtime.

  • Rust automatically calls drop function to return memory when variable goes out of scope.

  1. Call String::from

  2. It requests the memory it needs

  3. Drop returns memory automatically

About the Author

Mansoor Ahmed Chemical Engineer,Web developer

Rate this Article
Leave a Comment
Author Thumbnail
I Agree:
Comment 
Pictures
Author: Mansoor Ahmed

Mansoor Ahmed

Member since: Oct 10, 2020
Published articles: 124

Related Articles