Understanding Lifetime in Rust – Part I

I estimate that bulk of the time you invest in learning Rust will go into learning lifetime management. If we can keep aside Foreign Function Interface for a second, the rest of the language is just as simple to learn as Swift.

As I learn more about lifetime I plan on rolling out more articles in this series. In Part I we will explore one of the simplest use cases of lifetime.

Continue reading

Advertisements

Rust Using Visual Studio Code

I am getting decent productivity using MS Visual Studio Code to code Rust. This is how I have it setup.

Use a Good Font

VS Code has built in support for Rust syntax coloring. But a good font always helps. My favorite is Source Code Pro. Download and install it from Google Fonts.

In VS Code open preferences using the menu Code > Preferences > User Settings. The right hand pane has your personal settings. Enter the font settings as follows.

// Place your settings in this file to overwrite the default settings
{
    "editor.fontFamily": "SourceCodePro-Medium",
    "editor.fontSize": 18
}

image

Save changes.

Setup Build Tasks

With this setup you will be able to compile, run and test right from VS Code.

In VS Code press Command+Shift+P to open the available menu items.

image

Type task to filter the list.

Select Tasks: Configure Task Runner.

Enter this JSON in the editor. Courtesy various posters in this Reddit post.

{
    "version": "0.1.0",
    "command": "cargo",
    "isShellCommand": true,
    "tasks": [
        {
            "taskName": "build",
            "isBuildCommand": true,
            "showOutput": "always",
            "problemMatcher": {
                "owner": "rust",
                "fileLocation": [
                    "relative",
                    "${workspaceRoot}"
                ],
                "pattern": {
                    "regexp": "^(.*):(\d+):(\d+):\s+(\d+):(\d+)\s+(warning|error):\s+(.*)$",
                    "file": 1,
                    "line": 2,
                    "column": 3,
                    "endLine": 4,
                    "endColumn": 5,
                    "severity": 6,
                    "message": 7
                }
            }
        },
        {
            "taskName": "clean",
            "showOutput": "always"
        },
        {
            "taskName": "run",
            "showOutput": "always"
        },
        {
            "taskName": "test",
            "showOutput": "always",
            "isTestCommand": true,
            "problemMatcher": [
                {
                    "owner": "rust",
                    "fileLocation": [
                        "relative",
                        "${workspaceRoot}"
                    ],
                    "pattern": {
                        "regexp": "^(.*):(\d+):(\d+):\s+(\d+):(\d+)\s+(warning|error):\s+(.*)$",
                        "file": 1,
                        "line": 2,
                        "column": 3,
                        "endLine": 4,
                        "endColumn": 5,
                        "severity": 6,
                        "message": 7
                    }
                },
                {
                    "owner": "rust",
                    "fileLocation": [
                        "relative",
                        "${workspaceRoot}"
                    ],
                    "severity": "error",
                    "pattern": {
                        "regexp": "^.*panicked\s+at\s+'(.*)',\s+(.*):(\d+)$",
                        "message": 1,
                        "file": 2,
                        "line": 3
                    }
                }
            ]
        }
    ]
}

Save changes.

Configure Keyboard

VS Code already comes with keyboard shortcut for tasks like build (Command+Shift+B) and test (Command+Shift+t). But we will now setup a keyboard shortcut to run any task from the list of available tasks.

From VS Code menu select Code > Preferences > Keyboard Shortcuts.

In the right hand side pane enter:

// Place your key bindings in this file to overwrite the defaults
[
    { "key": "shift+cmd+r", "command": "workbench.action.tasks.runTask" }
]

Save changes.

Development Workflow

The tasks we have setup above works with Cargo based projects. From VS Code menu select File > Open. Select the root folder of your Cargo based project (the folder that contains Cargo.toml).

To do a build use Command+Shift+B. Errors and warnings will be highlighted in the editor.

image

You can also move the mouse over the error squiggly line and view the error message right there.

To test the crate press Command+Shift+t.

If you are working on a binary crate and want to run it press Command+Shift+r. This will open the list of all tasks.

image

Select run from the list.

Change Background Color

I use the dark theme (View > Theme > Dark theme). But the background is semi transparent which I find hugely distracting. To change the background, locate the file native.main.css within VS Code distribution. Open the file. Change the background to full black like this:

.monaco-editor.vs-dark .zone-widget .monaco-editor {
    color: #BBB;
    background: #000000;
}

Save changes.