Jon Deans Rust: Scalar Deepdive
A View on Scalars in Rust
Ferris is love Ferris is life
Scalars in Rust are Primitive Data Types that are composed of 3 main groups:
- Booleans
- Integers
- Characters
Scalars are known as well Scalars, because they hold singular values, but scale to a different value and in addition to this, Scalars hold a singular data value. 1
is a Scalar Value, "A"
is a Scalar Value, and True
can be a Scalar Value. This Articles main goal in mind is to understand what these Scalar Values are and are appended by examples of In-Code Rust.
Booleans in Rust
Sidenote: This article will not be covering Bitwise Boolean operators which are a beast for a separate discussion.
Boolean States
In rust there are two numerical States for Boolean values
True :: True parsed to an integer returns 1.
False :: False parsed to an integer returns 0.
Boolean Operators
In rust operators that allow us to manipulate Boolean logic come as Binary and Unary operators.
Unary
1. Logical NOT: The Boolean value to the right of this operator is reversed.
- Operator:
!Expression
::LOGICAL NOT
Example:
fn main() { let y = true; if !y{ println!("Y is equal to False"); }else{ println!("Y is equal to True"); }}// Output: Y is equal to True.
Binary
1. Is Equal To: The Operator on the left is compared to the Operator on the Right, if the values are equal to each other then the result is true.
- Operator:
Expression == Expression
::IS EQUAL TO
Example:
fn main() { let y = true; if y{ println!(“Y is equal to True”); }else{ println!(“Y is equal to False”); }}// Output: Y is equal to True
2. Logical AND: IF both values are true then the output will be true.
- Operator:
Expression && Expression
::LOGICAL AND
Example:
fn main() { let y = 0; let x = 1; if y == 0 && x == 1{ println!("Y and X evaluate to true"); }else{ println!("Either Y or X are false, or both are False"); }}// Output: Y and X evaluate to true
3. Logical OR: IF either values are true OR both values are true then the output will be true.
- Operator:
Expression || Expression
::LOGICAL OR
Example:
fn main() { let y = 0; let x = 1; if y == 0 || x == 1{ println!(“Y or X evaluate to true”); }else{ println!(“Neither Y nor X are true, so both are False”); }}// Output: Y and X evaluate to true
4. Logical NAND: IF NEITHER values are true OR a SINGULAR value is true then the output will be true.
- Operator:
Expression && !Expression
::LOGICAL NOT + LOGICAL AND
Example:
fn main() { let x = true; let y = false; if x && !y{ println!(“Y and X evaluate to true”); }else{ println!(“Either Y or X are false, so both are False”); }}// Output: Y and X evaluate to true
5. Logical NOR: IF NEITHER value is true then the output will be true.
- Operator:
Expression || !Expression
::LOGICAL NOT + LOGICAL OR
Example:
fn main() { let x = 3; let y = 2; if y == 2 || x == 2{ println!(“Y or X evaluate to true”); }else{ println!(“Either Y or X are false, or both are False”); }}// Output: Y is equal to True
6. Is Not Equal To:
- Operator:
Expression != Expression
::IS NOT EQUAL TO
Example:
fn main() { let y = true; if y != false{ println!(“Y is equal to True”); }else{ println!(“Y is equal to False”); }}// Output: Y is equal to True
7. Greater Than
- Operator:
Expression > Expression
::IS GREATER THAN
Example:
fn main() { let y = 1; if y > 0{ println!(“Y evaluates to True”); }else{ println!(“Y evaluates to False”); }}// Output: Y evaluates to True
8. Less than
- Operator:
Expression < Expression
::IS LESS THAN
Example:
fn main() { let y = 1; if y < 2{ println!(“Y evaluates to True”); }else{ println!(“Y evaluates to False”); }}// Output: Y evaluates to True
9. Greater Than or Equal To:
- Operator:
Expression >= Expression
::IS GREATER THAN OR EQUAL TO
Example:
fn main() { let y = 20; if y >= 20{ println!(“Y evaluates to True”); }else{ println!(“Y evaluates to False”); }}// Output: Y evaluates to True
10. Less Than or Equal To:
- Operator:
Expression <= Expression
::IS LESS THAN OR EQUAL TO
Example:
fn main() { let y = 1; if y <= 20{ println!(“Y evaluates to True”); }else{ println!(“Y evaluates to False”); }}// Output: Y evaluates to True
Integers in Rust
In Rust, we have the ability to assign numerical values to a type of data known as an Integer Type. Integers require an amount (ex: 1, 2, 3, etc.); also it requires us to specify the accuracy which is known as precision.
What Integer Types are available?
In rust we have Integer specifications which are known as Signed and Unsigned variables. These allow us to dictate our Range
- Signed Variables store positive and negative integers.
- Unsigned variables store Positive integers with a slightly larger value comparative to their Signed counter parts.
Signed and Unsigned Integers
Signed Integers
Negative Signed Integers
Positive Signed Integers
Unsigned Integers
What if we cast a variable without calling it’s type?
let x = 32 // The Immutable Variable of X has a value of 32.
What is Precision and How does it affect my Integers?
Precision affects how large of a Value your Variable can store, many people may never have to switch away from the generic i32.
When you’re trying to quantify data that exists in exorbitant amounts such as 4,000,000,000,000
, i32 will not be sufficient as it’s maximum possible positive value is 2,147,483,647. Because of this we have the option of using another integer type which is known as i128 (or u128) which has the maximum upper limit of 2¹²⁸, exactly what we need!
However there is a tax to using these Long Integers — which are i64, u64, i128, u128 -, and that is in the form of increased memory usage when the variable is being stored.
Strings and Characters in Rust
In Rust to represent ASCII or some form of Unicode Text we use Characters. In rust there are connected chains of characters known as Strings in order to convey words or to demonstrate symbols of meaning.
1. Char
A character in Rust is a Scalar Data Type that represents a Symbol such as A
, B
, or C
. These Symbols are surrounded by either “Double Quotes”
, ‘Single Quotes’
, or `Back Ticks`
.
2. String
A String is an outlier here and is actually not a Scalar Primitive but in Rust it is known as something called a Dynamically Sized Datatype. This is essentially a Collection of Characters that does not have a set size and is able to be increased in length or shortened in length, these actions come from it’s innate feature as being an Array of Characters.
What is a Char and what is a String?
In Rust a char is a singular Character represented by a Text Symbol surrounded by quotes “ “/’ ‘
.
ex:
fn main() {let char = “c”;println!(“{}”, char);}// Output: c
In Rust a String is a collection of Characters represented by a series of Text Symbols surrounded by quotes “ “/’ ‘
.
ex:
fn main() {let string = “Hello!”;println!(“{}”, string);}//Output: Hello!
Great Sources On Booleans:
Booleans in Rust
Operators in Rust
Great Video on Logic Gates
https://www.youtube.com/watch?v=gI-qXk7XojA
A Base look on Logic Gates
Great Sources on Integers:
Oregonstate pdf on Precision
Default of unspecified INTEGER type in Rust
Why does a byte only have 0 to 255?
Broad look on Integer Types in Rust
Downside to using Long Integers?
Calculations done on Desmos