mirror of
https://github.com/carlospolop/hacktricks
synced 2024-11-22 12:43:23 +00:00
GitBook: [#3282] No subject
This commit is contained in:
parent
9d3209aeae
commit
055ca84185
2 changed files with 220 additions and 0 deletions
|
@ -640,6 +640,7 @@
|
|||
## ✍ TODO
|
||||
|
||||
* [Other Big References](misc/references.md)
|
||||
* [Rust Basics](todo/rust-basics.md)
|
||||
* [More Tools](todo/more-tools.md)
|
||||
* [MISC](todo/misc.md)
|
||||
* [Pentesting DNS](pentesting-dns.md)
|
||||
|
|
219
todo/rust-basics.md
Normal file
219
todo/rust-basics.md
Normal file
|
@ -0,0 +1,219 @@
|
|||
# Rust Basics
|
||||
|
||||
### Generic Types
|
||||
|
||||
Create a struct where 1 of their values could be any type
|
||||
|
||||
```rust
|
||||
struct Wrapper<T> {
|
||||
value: T,
|
||||
}
|
||||
|
||||
impl<T> Wrapper<T> {
|
||||
pub fn new(value: T) -> Self {
|
||||
Wrapper { value }
|
||||
}
|
||||
}
|
||||
|
||||
Wrapper::new(42).value
|
||||
Wrapper::new("Foo").value, "Foo"
|
||||
```
|
||||
|
||||
### Option, Some & None
|
||||
|
||||
The Option type means that the value might by of type Some (there is something) or None:
|
||||
|
||||
```rust
|
||||
pub enum Option<T> {
|
||||
None,
|
||||
Some(T),
|
||||
}
|
||||
```
|
||||
|
||||
You can use functions such as `is_some()` __ or __ `is_none()` to check the value of the Option.
|
||||
|
||||
### Conditionals
|
||||
|
||||
#### if
|
||||
|
||||
```rust
|
||||
let n = 5;
|
||||
if n < 0 {
|
||||
print!("{} is negative", n);
|
||||
} else if n > 0 {
|
||||
print!("{} is positive", n);
|
||||
} else {
|
||||
print!("{} is zero", n);
|
||||
}
|
||||
```
|
||||
|
||||
#### match
|
||||
|
||||
```rust
|
||||
match number {
|
||||
// Match a single value
|
||||
1 => println!("One!"),
|
||||
// Match several values
|
||||
2 | 3 | 5 | 7 | 11 => println!("This is a prime"),
|
||||
// TODO ^ Try adding 13 to the list of prime values
|
||||
// Match an inclusive range
|
||||
13..=19 => println!("A teen"),
|
||||
// Handle the rest of cases
|
||||
_ => println!("Ain't special"),
|
||||
}
|
||||
|
||||
let boolean = true;
|
||||
// Match is an expression too
|
||||
let binary = match boolean {
|
||||
// The arms of a match must cover all the possible values
|
||||
false => 0,
|
||||
true => 1,
|
||||
// TODO ^ Try commenting out one of these arms
|
||||
};
|
||||
```
|
||||
|
||||
#### loop (infinite)
|
||||
|
||||
```rust
|
||||
loop {
|
||||
count += 1;
|
||||
if count == 3 {
|
||||
println!("three");
|
||||
continue;
|
||||
}
|
||||
println!("{}", count);
|
||||
if count == 5 {
|
||||
println!("OK, that's enough");
|
||||
break;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### while
|
||||
|
||||
```rust
|
||||
let mut n = 1;
|
||||
while n < 101 {
|
||||
if n % 15 == 0 {
|
||||
println!("fizzbuzz");
|
||||
} else if n % 5 == 0 {
|
||||
println!("buzz");
|
||||
} else {
|
||||
println!("{}", n);
|
||||
}
|
||||
n += 1;
|
||||
}
|
||||
```
|
||||
|
||||
#### for
|
||||
|
||||
```rust
|
||||
for n in 1..101 {
|
||||
if n % 15 == 0 {
|
||||
println!("fizzbuzz");
|
||||
} else {
|
||||
println!("{}", n);
|
||||
}
|
||||
}
|
||||
|
||||
// Use "..=" to make inclusive both ends
|
||||
for n in 1..=100 {
|
||||
if n % 15 == 0 {
|
||||
println!("fizzbuzz");
|
||||
} else if n % 3 == 0 {
|
||||
println!("fizz");
|
||||
} else if n % 5 == 0 {
|
||||
println!("buzz");
|
||||
} else {
|
||||
println!("{}", n);
|
||||
}
|
||||
}
|
||||
|
||||
// ITERATIONS
|
||||
|
||||
let names = vec!["Bob", "Frank", "Ferris"];
|
||||
//iter - Doesn't consume the collection
|
||||
for name in names.iter() {
|
||||
match name {
|
||||
&"Ferris" => println!("There is a rustacean among us!"),
|
||||
_ => println!("Hello {}", name),
|
||||
}
|
||||
}
|
||||
//into_iter - COnsumes the collection
|
||||
for name in names.into_iter() {
|
||||
match name {
|
||||
"Ferris" => println!("There is a rustacean among us!"),
|
||||
_ => println!("Hello {}", name),
|
||||
}
|
||||
}
|
||||
//iter_mut - This mutably borrows each element of the collection
|
||||
for name in names.iter_mut() {
|
||||
*name = match name {
|
||||
&mut "Ferris" => "There is a rustacean among us!",
|
||||
_ => "Hello",
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### if let
|
||||
|
||||
```rust
|
||||
let optional_word = Some(String::from("rustlings"));
|
||||
if let word = optional_word {
|
||||
println!("The word is: {}", word);
|
||||
} else {
|
||||
println!("The optional word doesn't contain anything");
|
||||
}
|
||||
```
|
||||
|
||||
#### while let
|
||||
|
||||
```rust
|
||||
let mut optional = Some(0);
|
||||
// This reads: "while `let` destructures `optional` into
|
||||
// `Some(i)`, evaluate the block (`{}`). Else `break`.
|
||||
while let Some(i) = optional {
|
||||
if i > 9 {
|
||||
println!("Greater than 9, quit!");
|
||||
optional = None;
|
||||
} else {
|
||||
println!("`i` is `{:?}`. Try again.", i);
|
||||
optional = Some(i + 1);
|
||||
}
|
||||
// ^ Less rightward drift and doesn't require
|
||||
// explicitly handling the failing case.
|
||||
}
|
||||
```
|
||||
|
||||
### Traits
|
||||
|
||||
Create a new method for a type
|
||||
|
||||
```rust
|
||||
trait AppendBar {
|
||||
fn append_bar(self) -> Self;
|
||||
}
|
||||
|
||||
impl AppendBar for String {
|
||||
fn append_bar(self) -> Self{
|
||||
format!("{}Bar", self)
|
||||
}
|
||||
}
|
||||
|
||||
let s = String::from("Foo");
|
||||
let s = s.append_bar();
|
||||
println!("s: {}", s);
|
||||
```
|
||||
|
||||
### Tests
|
||||
|
||||
```rust
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
fn you_can_assert() {
|
||||
assert!(true);
|
||||
assert_eq!(true, true);
|
||||
}
|
||||
}
|
||||
```
|
Loading…
Reference in a new issue