Rust – Hello World

main.rs

  1. fn main() {
  2.     println!("Hello, world!");
  3.     let a = "Water";
  4.     println!("Hello, {}!", a);
  5. }

tasks.json

  1. { 
  2.     "version":"2.0.0", 
  3.     "tasks":[ 
  4.         { 
  5.             "label":"build", 
  6.             "type":"shell", 
  7.             "command":"cargo", 
  8.             "args":["build"] 
  9.         } 
  10.     ] 
  11. }

launch.json

  1. { 
  2.         "version":"0.2.0", 
  3.         "configurations":[ 
  4.             { 
  5.                 "name":"(Windows) 启动", 
  6.                 "preLaunchTask":"build", 
  7.                 "type":"cppvsdbg", 
  8.                 "request":"launch", 
  9.                 "program":"${workspaceFolder}/target/debug/${workspaceFolderBasename}.exe", 
  10.                 "args":[], 
  11.                 "stopAtEntry":false, 
  12.                 "cwd":"${workspaceFolder}", 
  13.                 "environment":[], 
  14.                 "externalConsole":false 
  15.             }, 
  16.             { 
  17.                 "name":"(gdb) 启动", 
  18.                 "type":"cppdbg", 
  19.                 "request":"launch", 
  20.                 "program":"${workspaceFolder}/target/debug/${workspaceFolderBasename}.exe", 
  21.                 "args":[], 
  22.                 "stopAtEntry":false, 
  23.                 "cwd":"${workspaceFolder}", 
  24.                 "environment":[], 
  25.                 "externalConsole":false, 
  26.                 "MIMode":"gdb", 
  27.                 "miDebuggerPath":"这里填GDB所在的目录", 
  28.                 "setupCommands":[ 
  29.                     { 
  30.                         "description":"为 gdb 启用整齐打印", 
  31.                         "text":"-enable-pretty-printing", 
  32.                         "ignoreFailures":true 
  33.                     } 
  34.                 ] 
  35.             } 
  36.         ] 
  37. }

Calling a C Function From Lua

Prepare So

  1. #include <math.h>
  2. #include <lua.h>
  3. #include <lauxlib.h>
  4. #include <lualib.h>
  5.  
  6. static int my_sum(lua_State *L){
  7.     int d1 = luaL_checknumber(L, 1);
  8.     int d2 = luaL_checknumber(L, 2);
  9.     lua_pushnumber(L, d1+d2);
  10.     return 1;
  11. }
  12.  
  13. static int my_info(lua_State *L){
  14.     lua_pushstring(L, "qinuu");
  15.     return 1;
  16. }
  17.  
  18. static const struct luaL_Reg test_lib[] = {
  19.     {"my_sum" , my_sum},
  20.     {"my_info" , my_info},
  21.     {NULL, NULL}
  22. };
  23.  
  24. int luaopen_test_lib(lua_State *L){
  25.     //luaL_newlib(L, test_lib); // 5.2
  26.     luaL_register(L, "test_lib",test_lib); // lua 5.1
  27.     return 1;
  28. }

Test

  1. local my_lib = require "test_lib"
  2.  
  3. print(type(test_lib))
  4.  
  5. print(test_lib.my_sum(23,17))
  6.  
  7. print(test_lib.my_info())