DEV Community

nozomi-iida
nozomi-iida

Posted on

1 1

How to do LeetCode with Rust in Vim

I solve problems in LeetCode everyday, so I tried to set up LeetCode and Rust to write with vim.

Submit LeetCode through terminal

1. Install leetcode-cli through npm.

npm install -g leetcode-cli 
Enter fullscreen mode Exit fullscreen mode

2. Update config.json

The config file was created in ~/.lc/config.json.
Update lang to set default language used in coding.

{
  "code": {
    "lang": "rust"
  }
}
Enter fullscreen mode Exit fullscreen mode

3. Create directory and download problem

show command is used to generate source file in .leetcode

mkdir ~/.leetcode
cd ~/.leetcode
leetcode show -g ${question by name or id}
Enter fullscreen mode Exit fullscreen mode

4. Run test command to confirm your code

After writing code, you should run test command before submit.

leetcode test ${file name}
Enter fullscreen mode Exit fullscreen mode

Run submit command

Once the tests pass, submit your code to LeetCode.

You could solve problem in LeetCode with only terminal!

Setup Rust in vim

1. Add mason.nvim and mason-lspconfig.nvim

mason is a package manager specializing in language-related functions, for example lsp.

2. Set mason config

Set the server configured in lspconfig to be installed automatically

~/nvim/after/plugin/mason.

local status, mason = pcall(require, "mason")
if (not status) then return end
local status2, lspconfig = pcall(require, "mason-lspconfig")
if (not status2) then return end

mason.setup({})

lspconfig.setup {
  automatic_installation = true
}
Enter fullscreen mode Exit fullscreen mode

3. Set lspconfig

Add rust-analyzer in lspconfig.

local status, nvim_lsp = pcall(require, "lspconfig")
if (not status) then return end

-- Rust
nvim_lsp.rust_analyzer.setup{
  filetypes = { "rust" }
}
Enter fullscreen mode Exit fullscreen mode

After this setting, you can

4. Add rust-tools

local status, rust_tools = pcall(require, "rust-tools")
if (not status) then return end

rust_tools.setup({
  tools = {
    autoSetHints = true,
  },
  server = {},
})
vim.g.rustfmt_autosave = 1
Enter fullscreen mode Exit fullscreen mode

autoSetHints is very useful.
it shows variable type.
Image description

I add vim.g.rustfmt_autosave = 1 to format automatically when I save files.

Summarize

Finally, I can comfortably write Rust in vim and submit code to LeetCode through terminal.

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (0)

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay