Skip to content

Instantly share code, notes, and snippets.

@zaynkorai
Created June 18, 2019 12:58
Show Gist options
  • Save zaynkorai/a9db5afa723ee672563d2e760d876eb3 to your computer and use it in GitHub Desktop.
Save zaynkorai/a9db5afa723ee672563d2e760d876eb3 to your computer and use it in GitHub Desktop.
-- Rust is a statically typed language, which means that it must know the types of all variables at compile time. The compiler can usually infer what type we want to use based on the value and how we use it. // &T
-- fn main() {
let mut x = "A";
let x = x.len();
println!("{}",x);
} //compile error
-- In function signatures, you must declare the type of each parameter.&T
-- You can return early from a function by using the return keyword and specifying a value, but most functions return the last expression implicitly. &T
-- A tuple is a general way of grouping together some number of other values with a variety of types into one compound type. Tuples have a fixed length: once declared, they cannot grow or shrink in size. &T
-- Rust’s defaults are generally good choices, and integer types default to i32
-- fn main() {
let x = 5;
let x = x + 1;
let x = x * 2;
println!("The value of x is: {}", x);
} // The value of x is: 12
-- fn main() {
let mut number =3;
while number != 0{
println!("{}",number);
number = number-1;
}
} // 3 2 1
-- You can return early from a function by using the return keyword and specifying a value, but most functions return the last expression implicitly. &T
-- Rust has two primitive compound types: tuples and arrays.
Compound types can group multiple values into one type.
-- fn main() {
let a = [10,20,30];
for element in a {
println!("{}",element);
}
} // compile error
-- difference between mut and shadowing is that because we’re effectively creating a new variable when we use the let keyword again, we can change the type of the value but reuse the same name. &T
-- fn main() {
let number = 3;
if number {
println!("number was three");
}
} // compile error
-- Comments must start with two slashes and continue until the end of the line
For comments that extend beyond a single line, you’ll need to include // on each line.
Comments can also be placed at the end of lines containing code.
-- fn main() {
let x = (let y = 6);
} // compile error, variable declaration using `let` is a statement
-- Rust also has two primitive types for floating-point numbers, Float types default to f64
-- Constants may be set only to a constant expression, not the result of a function call or any other value that could only be computed at runtime. &T
-- Constants can be declared in any scope, including the global scope, which makes them useful for values that many parts of code need to know about. &T
-- you declare constants using the const keyword instead of the le keyword, and the compiler itself the type of the value and the type need not be defined &F
-- fn main() {
let mut counter = 0;
let result = loop {
counter += 1;
if counter == 5 {
break counter * 2;
}
};
println!("The result is {}", result);
} // result is 10
-- A scalar type represents a single value. Rust has four primary scalar types: integers, floating-point numbers, Booleans, and characters.
-- fn main() {
let a = [1, 2, 3, 4, 5];
let element = a[10];
println!("{}",element)
} // compile error, index out of bound
-- fn main() {
let mut x = 5;
println!("The value of x is: {}", x);
x = 6;
println!("The value of x is: {}", x);
} // 5, 6
-- fn main() {
let x = 5;
println!("The value of x is: {}", x);
x = 6;
println!("The value of x is: {}", x);
} // compile error, x is not mut
-- we can access a tuple element directly by using a period (.) followed by the index of the value we want to access. tup.0
-- fn main() {
for number in (1..4).rev() {
println!("{}!", number);
}
println!("LIFTOFF!!!");
} // 3, 2, 1
-- fn main() {
let condition = true;
let number = if condition {
5
} else {
6
};
println!("The value of number is: {}", number);
} // 5
-- fn main() {
let condition = true;
let number = if condition {
5
} else {
"six"
};
println!("The value of number is: {}", number);
} // compile error , expected integral variable, found &str
-- Expressions do not include ending semicolons. If you add a semicolon to the end of an expression, you turn it into a statement, which will then not return a value. &T
-- Rust code uses *snake* case as the conventional style for function and variable names. In snake case, all letters are lowercase and underscores separate words.
-- To get the individual values out of a tuple, we can use pattern matching to destructure a tuple value, i.e let (x, y, z) = tup;
-- Creatong a variable and assigning a value to it with let keyword is an expression &F
-- Constants aren’t just immutable by default—they’re mutable only when we define them as such. &F // they’re always immutable. you aren’t allowed to use mut with constants.
-- An Array is a similar collection type provided in rust that is allowed to grow or shrink in size. &F Arrays in Rust have a fixed length.
-- Calling a function is an expression. Calling a macro is an expression. The block that we use to create new scopes, {}, is an expression,
-- fn main() {
let number = 6;
if number % 4 == 0 {
println!("number is divisible by 4");
} else if number % 3 == 0 {
println!("number is divisible by 3");
} else if number % 2 == 0 {
println!("number is divisible by 2");
} else {
println!("number is not divisible by 4, 3, or 2");
}
} // number is divisible by 3
-- fn main() {
let x = 'A';
let x = x.len();
println!("{}",x);
} // compile error
-- Statements* are instructions that perform some action and do not return a value. Expressions* evaluate to a resulting value.
-- Consider a simple math operation, such as 5 + 6, which is an expression that evaluates to the value 11 &T
-- Arrays are useful when you want your data allocated on the stack rather than the heap &T
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment