The Rust Programming Language
- Variables and Mutability
- By default, variables are immutable -> safety & easy concurrency advantages
- Adding mut in front of the variable name makes variable from immutable to mutable
- constants
- not allowed to use mut keyword with constants
- always immutable
- using the const keyword to make constants instead of let keyword
- the type of the value must be annotated
- convention: upper case (all capital letters) with underscores between words
- ex) user's acquired MAX_POINTS in a stage, the speed of the light, etc.
- shadowing
- we can declare a new variable with the same name as a previous variable
- shadowing vs. mut
- using shadowing and after transformations, it keeps the variable be immutable
- we can change the type of the value but reuse the same name using shadowing
- mut keyword doesn't ensure that mutating a variable's type
- Data Types
- Rust is a statically typed language (type inference available)
- Scalar Types
- represents a single value
- Integer Types
- a number without a fractional component
- signed numbers are stored using two's complement representation
- isize and usize types depend on the architecture of the computer
- number literals that can be multiple numeric types allow a type suffix (ex. 57u8)
- can also use _ as a visual separator
- the primary situation that use isize or usize is when indexing some sort of collection
- integer default type is i32
- Floating-Point Types
- f32 (single-precision float), f64 (double-precision float)
- floating point default type is f64
- all floating point types are signed
- The Boolean Type
- true, false
- 1 byte in size
- type anotation is bool
- The Character Type
- type anotation is char
- single quotes
- 4 bytes in size
- represents Unicode Scalar
- Compound Types
- The Tuple Type
- grouping a number of values
- fixed length
- (,)
- number types can different
- destructuring available
- can access a tuple element directly by using a period
- variable convention: snake_case
- The tuple without any values has a special name => unit
- unit: empty value or empty return type
- The Array Type
- every element of an array must have the same type
- fixed length
- allocate on the stack
- array isn't as flexible as the vector type
- arrays are more useful when we know the number of elements will not need to be changed
ex) 12 month names
- array type annotation: [type/same value; number]
- index out of bounds error will be captured at a runtime not a compile time (panic mode)
- Functions
- main fn => entry point of many programs
- function name convention: snake_case
- parameters
- we must declare the type of each parameter
- compiler almost never needs us to use them elsewhere in the code
to figure out what type we mean
- compiler is also able to give more helpful error messages if it know what types the function expects
- parameters are not needs to be the same type
- statements and expressions
- Rust is an expression-based language
- statement vs. expression
- let statement, function statement, ... does not return a value
- expression returns a value
- expressions can be part of statements
- a new scope block created with curly brackets is an expression, also
- expressions do not include ending semicolons
(if include ending semicolons -> turn it into a statement)
- functions with return values
- don't name return values, but we must declare return type using right arrow (->)
- Comments
- // (one line comment)
- /// (documentation comment)
- Control Flow
- if expressions
- called arms(branches)
- condition expression return value must be a bool type not just allow the number type
- Rust will not automatically try to convert non-Boolean types to a Boolean
- using if in a let statement
- if is an expression
- return results from each arm of the if must be the same type
(compiler must knows that variable's type in compile time not a runtime)
- repetition with loops
- loop, while, for
- repeating code with loop
- infinite repetition
- use break keyword to stop
- we can return after the break expression with ending semicolon (only for loop statement)
- can use continue keyword to skip current iteration and go to the next iteration
- loop label must begin with a single quote (loop label -> disambiguate between multiple loops)
- conditional loops with while
- while the condition is true, the loop runs (otherwise exit the loop)
- looping through a collection with for
- for number in array.iter() { }
- run some code a certain number of times
- Range (standard library)
- for number in (1..4).rev() { }
(range of the end number is exclusive)
'Personal-Study > Rust' 카테고리의 다른 글
[Rust 문서 읽기] 5. Using Structs to Structure Related Data (연관된 데이터들을 구조체로 다루기) (0) | 2023.02.05 |
---|---|
[Rust 문서 읽기] 4. Understanding Ownership (소유권 이해하기) (0) | 2023.01.29 |
[Rust 문서 읽기] 2. Programming a Guessing Game (추리 게임 튜토리얼) (0) | 2023.01.29 |
[Rust 문서 읽기] 1. Getting Started (시작하기) (0) | 2023.01.28 |
[Rust 문서 읽기] Introduction (소개) (0) | 2023.01.28 |
댓글