Skip to content

Instantly share code, notes, and snippets.

@trungnt13
Last active January 3, 2026 14:09
Show Gist options
  • Select an option

  • Save trungnt13/ce294ef7061c84868e92d77e1eeb2740 to your computer and use it in GitHub Desktop.

Select an option

Save trungnt13/ce294ef7061c84868e92d77e1eeb2740 to your computer and use it in GitHub Desktop.

Revisions

  1. trungnt13 revised this gist Jun 11, 2024. No changes.
  2. trungnt13 revised this gist Jun 11, 2024. No changes.
  3. trungnt13 revised this gist Jun 11, 2024. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions Rust.md
    Original file line number Diff line number Diff line change
    @@ -89,8 +89,6 @@ Too many `Cargo.toml` files in the workspace, set this:
    "CARGO_HOME": "/home/tngo/codes/ml3x/insim/build/.cargo"
    },
    ```


    ---

    ## Control Flow
  4. trungnt13 revised this gist Jun 11, 2024. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions Rust.md
    Original file line number Diff line number Diff line change
    @@ -4,6 +4,7 @@
    - [VSCode and rust-analyzer](#vscode-and-rust-analyzer)
    - [rust-analyzer failed to discover workspace](#rust-analyzer-failed-to-discover-workspace)
    - [rust-analyzer block build](#rust-analyzer-block-build)
    - [rust-analyzer CARGO\_HOME](#rust-analyzer-cargo_home)
    - [Control Flow](#control-flow)
    - [If-then-else](#if-then-else)
    - [Match](#match)
    @@ -81,6 +82,15 @@ Too many `Cargo.toml` files in the workspace, set this:
    ]
    ```

    ### rust-analyzer CARGO_HOME

    ```json
    "rust-analyzer.server.extraEnv": {
    "CARGO_HOME": "/home/tngo/codes/ml3x/insim/build/.cargo"
    },
    ```


    ---

    ## Control Flow
  5. trungnt13 revised this gist Jun 10, 2024. No changes.
  6. trungnt13 revised this gist Jun 10, 2024. No changes.
  7. trungnt13 revised this gist Jun 10, 2024. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions Rust.md
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,7 @@
    - [Rust](#rust)
    - [VSCode and rust-analyzer](#vscode-and-rust-analyzer)
    - [rust-analyzer failed to discover workspace](#rust-analyzer-failed-to-discover-workspace)
    - [rust-analyzer block build](#rust-analyzer-block-build)
    - [Control Flow](#control-flow)
    - [If-then-else](#if-then-else)
    - [Match](#match)
  8. trungnt13 revised this gist Jun 5, 2024. No changes.
  9. trungnt13 revised this gist Jun 5, 2024. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion Rust.md
    Original file line number Diff line number Diff line change
    @@ -75,7 +75,9 @@ Too many `Cargo.toml` files in the workspace, set this:
    ### rust-analyzer block build

    ```json
    "rust-analyzer.cargo.loadOutDirsFromCheck": true
    "rust-analyzer.check.extraArgs": [
    "--target-dir=target/rust-analyzer"
    ]
    ```

    ---
  10. trungnt13 revised this gist Jun 5, 2024. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion Rust.md
    Original file line number Diff line number Diff line change
    @@ -78,7 +78,6 @@ Too many `Cargo.toml` files in the workspace, set this:
    "rust-analyzer.cargo.loadOutDirsFromCheck": true
    ```


    ---

    ## Control Flow
  11. trungnt13 revised this gist Jun 5, 2024. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions Rust.md
    Original file line number Diff line number Diff line change
    @@ -72,6 +72,13 @@ Too many `Cargo.toml` files in the workspace, set this:
    ]
    ```

    ### rust-analyzer block build

    ```json
    "rust-analyzer.cargo.loadOutDirsFromCheck": true
    ```


    ---

    ## Control Flow
  12. trungnt13 revised this gist May 28, 2024. No changes.
  13. trungnt13 revised this gist Apr 27, 2024. No changes.
  14. trungnt13 revised this gist Apr 27, 2024. No changes.
  15. trungnt13 revised this gist Apr 26, 2024. No changes.
  16. trungnt13 revised this gist Apr 26, 2024. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions Rust.md
    Original file line number Diff line number Diff line change
    @@ -241,6 +241,7 @@ fn pr(x: [Option<String>; 4]) {
    let y = x[0]; // ERROR
    let y = &x[0]; // OK
    let z = y.unwrap(); // ERROR: cannot move out of y because it's borrowed reference
    let z = (*y).unwrap(); // ERROR: same as above, automatic dereference here
    let z0: String = x[0].clone().unwrap(); // OK
    let z1: &String = x[0].as_ref().unwrap(); // OK
    }
  17. trungnt13 revised this gist Apr 26, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Rust.md
    Original file line number Diff line number Diff line change
    @@ -240,7 +240,7 @@ When you assign `x[0]` to `y`, you are moving the `Option<String>` out of the ar
    fn pr(x: [Option<String>; 4]) {
    let y = x[0]; // ERROR
    let y = &x[0]; // OK
    let z0 = y.unwrap(); // ERROR: cannot move out of y because it's borrowed reference
    let z = y.unwrap(); // ERROR: cannot move out of y because it's borrowed reference
    let z0: String = x[0].clone().unwrap(); // OK
    let z1: &String = x[0].as_ref().unwrap(); // OK
    }
  18. trungnt13 revised this gist Apr 26, 2024. No changes.
  19. trungnt13 revised this gist Apr 26, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Rust.md
    Original file line number Diff line number Diff line change
    @@ -240,7 +240,7 @@ When you assign `x[0]` to `y`, you are moving the `Option<String>` out of the ar
    fn pr(x: [Option<String>; 4]) {
    let y = x[0]; // ERROR
    let y = &x[0]; // OK
    let z0 = y.clone().unwrap(); // ERROR: cannot move out of y because it's borrowed reference
    let z0 = y.unwrap(); // ERROR: cannot move out of y because it's borrowed reference
    let z0: String = x[0].clone().unwrap(); // OK
    let z1: &String = x[0].as_ref().unwrap(); // OK
    }
  20. trungnt13 revised this gist Apr 26, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Rust.md
    Original file line number Diff line number Diff line change
    @@ -240,7 +240,7 @@ When you assign `x[0]` to `y`, you are moving the `Option<String>` out of the ar
    fn pr(x: [Option<String>; 4]) {
    let y = x[0]; // ERROR
    let y = &x[0]; // OK
    let z0 = y.clone().unwrap(); // ERROR: cannot move out of y because it's borrowed
    let z0 = y.clone().unwrap(); // ERROR: cannot move out of y because it's borrowed reference
    let z0: String = x[0].clone().unwrap(); // OK
    let z1: &String = x[0].as_ref().unwrap(); // OK
    }
  21. trungnt13 revised this gist Apr 26, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Rust.md
    Original file line number Diff line number Diff line change
    @@ -239,8 +239,8 @@ When you assign `x[0]` to `y`, you are moving the `Option<String>` out of the ar
    ```rust
    fn pr(x: [Option<String>; 4]) {
    let y = x[0]; // ERROR
    let y = x[0].unwrap(); // ERROR: cannot move out of x
    let y = &x[0]; // OK
    let z0 = y.clone().unwrap(); // ERROR: cannot move out of y because it's borrowed
    let z0: String = x[0].clone().unwrap(); // OK
    let z1: &String = x[0].as_ref().unwrap(); // OK
    }
  22. trungnt13 revised this gist Apr 26, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Rust.md
    Original file line number Diff line number Diff line change
    @@ -239,7 +239,7 @@ When you assign `x[0]` to `y`, you are moving the `Option<String>` out of the ar
    ```rust
    fn pr(x: [Option<String>; 4]) {
    let y = x[0]; // ERROR
    let y = x[0].unwrap(); // ERROR
    let y = x[0].unwrap(); // ERROR: cannot move out of x
    let y = &x[0]; // OK
    let z0: String = x[0].clone().unwrap(); // OK
    let z1: &String = x[0].as_ref().unwrap(); // OK
  23. trungnt13 revised this gist Apr 26, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Rust.md
    Original file line number Diff line number Diff line change
    @@ -239,7 +239,7 @@ When you assign `x[0]` to `y`, you are moving the `Option<String>` out of the ar
    ```rust
    fn pr(x: [Option<String>; 4]) {
    let y = x[0]; // ERROR
    let y = x[0]
    let y = x[0].unwrap(); // ERROR
    let y = &x[0]; // OK
    let z0: String = x[0].clone().unwrap(); // OK
    let z1: &String = x[0].as_ref().unwrap(); // OK
  24. trungnt13 revised this gist Apr 26, 2024. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions Rust.md
    Original file line number Diff line number Diff line change
    @@ -239,6 +239,8 @@ When you assign `x[0]` to `y`, you are moving the `Option<String>` out of the ar
    ```rust
    fn pr(x: [Option<String>; 4]) {
    let y = x[0]; // ERROR
    let y = x[0]
    let y = &x[0]; // OK
    let z0: String = x[0].clone().unwrap(); // OK
    let z1: &String = x[0].as_ref().unwrap(); // OK
    }
  25. trungnt13 revised this gist Apr 26, 2024. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions Rust.md
    Original file line number Diff line number Diff line change
    @@ -239,9 +239,8 @@ When you assign `x[0]` to `y`, you are moving the `Option<String>` out of the ar
    ```rust
    fn pr(x: [Option<String>; 4]) {
    let y = x[0]; // ERROR
    let z0: String = x[0].clone().unwrap();
    let z1 = x[0].as_ref().unwrap();

    let z0: String = x[0].clone().unwrap(); // OK
    let z1: &String = x[0].as_ref().unwrap(); // OK
    }
    ```

  26. trungnt13 revised this gist Apr 26, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Rust.md
    Original file line number Diff line number Diff line change
    @@ -239,7 +239,7 @@ When you assign `x[0]` to `y`, you are moving the `Option<String>` out of the ar
    ```rust
    fn pr(x: [Option<String>; 4]) {
    let y = x[0]; // ERROR
    let z0 = x[0].clone().unwrap();
    let z0: String = x[0].clone().unwrap();
    let z1 = x[0].as_ref().unwrap();

    }
  27. trungnt13 revised this gist Apr 26, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Rust.md
    Original file line number Diff line number Diff line change
    @@ -239,7 +239,7 @@ When you assign `x[0]` to `y`, you are moving the `Option<String>` out of the ar
    ```rust
    fn pr(x: [Option<String>; 4]) {
    let y = x[0]; // ERROR
    let z0 = x[0].clone().unwrap();
    let z0 = x[0].clone().unwrap();
    let z1 = x[0].as_ref().unwrap();

    }
  28. trungnt13 revised this gist Apr 26, 2024. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions Rust.md
    Original file line number Diff line number Diff line change
    @@ -239,6 +239,9 @@ When you assign `x[0]` to `y`, you are moving the `Option<String>` out of the ar
    ```rust
    fn pr(x: [Option<String>; 4]) {
    let y = x[0]; // ERROR
    let z0 = x[0].clone().unwrap();
    let z1 = x[0].as_ref().unwrap();

    }
    ```

  29. trungnt13 revised this gist Apr 26, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Rust.md
    Original file line number Diff line number Diff line change
    @@ -235,8 +235,8 @@ Compiler ensures:

    In Rust, variables are moved by default when you assign them to another variable or pass them to a function. This means that the original variable can no longer be used after the move.
    When you assign `x[0]` to `y`, you are moving the `Option<String>` out of the array. This is not allowed because the size of the array is fixed, and moving an element out would leave a hole.
    ```rust

    ```rust
    fn pr(x: [Option<String>; 4]) {
    let y = x[0]; // ERROR
    }
  30. trungnt13 revised this gist Apr 26, 2024. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions Rust.md
    Original file line number Diff line number Diff line change
    @@ -234,9 +234,9 @@ Compiler ensures:
    ### Move Out of an Array

    In Rust, variables are moved by default when you assign them to another variable or pass them to a function. This means that the original variable can no longer be used after the move.

    When you assign `x[0]` to `y`, you are moving the `Option<String>` out of the array. This is not allowed because the size of the array is fixed, and moving an element out would leave a hole.
    ```rust
    // When you assign `x[0]` to `y`, you are moving the `Option<String>` out of the array. This is not allowed because the size of the array is fixed, and moving an element out would leave a hole.

    fn pr(x: [Option<String>; 4]) {
    let y = x[0]; // ERROR
    }