Take input in Rust

I have this mapping for running rust in command mode

augroup rust_settings
    autocmd!
    au Filetype rust nnoremap <buffer> <silent> <Enter> :!cargo run <Cr>
augroup END

but it can’t take the user input
How can I change?
You can use the code below for testing:

use std::io;
fn main() {
    let mut input = String::new();
    io::stdin()
        .read_line(&mut input)
        .expect("Fail when readline");
    let num: u32 = input.trim().parse().unwrap();
    println!("{:?}", num);
}

You can try changing it to :terminal cargo run<CR>.

It works, thank you but how can I change it to look prettier?

Vào Th 5, 16 thg 12, 2021 vào lúc 13:20 zeertzjq via Neovim Discourse <neovim@discoursemail.com> đã viết:

Add the below codeblock to your config only if your config is in vimscript:

" ============== template / compile / run ====================

nnoremap <leader>t :!cp ~/templates/template.%:e %<Enter> 

"shortcut to run current filetype
map mr :call CompileRunGcc()<CR>
func! CompileRunGcc()
    exec "w"
    if &filetype == 'c'
        exec "!clear && gcc % -o %<"
        exec "!clear && time ./%<"
    elseif &filetype == 'cpp'
        exec "!clear && g++ % -o %<"
        exec "!clear && time ./%<"
    elseif &filetype == 'rust'
        exec "!clear && cargo build --manifest-path=%:p:h:h/Cargo.toml"
        exec "!time cargo run --manifest-path=%:p:h:h/Cargo.toml"
         
    "elseif &filetype == 'rust'
    "    exec "!clear && rustc %:p --out-dir=%:p:h"
    "    exec "!time " .expand("%:p:r")
    " elseif expand('%:t') == 'main.rs'
    "     exec "!clear && cargo build --manifest-path=%:p:h:h/Cargo.toml"
    "     exec "!time cargo run --manifest-path=%:p:h:h/Cargo.toml"

    elseif &filetype == 'java'
        exec "!clear && javac %"
        exec "!clear && time java -cp %:p:h %:t:r"
    elseif &filetype == 'sh'
        exec "!clear && time bash %"
    elseif &filetype == 'python'
        exec "!clear && time python3 %"
    elseif &filetype == 'javascript'
        exec "!clear && time node %"
    elseif &filetype == 'html'
        exec "!chromium % &"
    elseif &filetype == 'tex'
        exec "!pdflatex --output-directory=%:p:h %"
    elseif &filetype == 'go'
        exec "!go build %<"
        exec "!clear && time go run %"
    elseif &filetype == 'text'
        exec "!clear && echo 'words : ' && wc -w % && echo 'lines : ' && wc -l % && echo 'size : ' && du -h %"
    elseif &filetype == 'markdown'
        exec "!clear && echo 'words : ' && wc -w % && echo 'lines : ' && wc -l % && echo 'size : ' && du -h %"
    endif
endfunc

It can compile and run most of the file types but what’s special about this is that you don’t have to be in the package directory to be able to compile/run the program. So you can open main.rs in vim/neovim and compile/run it just fine, it won’t complain about cargo.toml not found.

Another way is to use rust.vim plugin which provides you RustRun global function which does what you need.

In case: Command line arguments are added right after RustRun.


It raised the error when I press mr and <leader>t also went wrong