[Nanny-Level Tutorial] How to write a ChatGPT desktop application with Rust | JD Cloud Technical Team

Why do we need a desktop app

There are so many reasons, we need to export records conveniently, we need to automatically add "continue" when the answer is too long, and we need to collect some cool and practical prompts... (First of all, I assume you are
a As I am used to java developed with IDEA)

Why is it written in Rust

  1. High efficiency and low resource consumption.
  2. High security: Rust is a memory-safe language, and all its operations have undergone strict code inspection at the system level, reducing the occurrence of crashes and memory leaks during runtime.
  3. High maintainability: Rust can better write maintainable code, and supports object-oriented programming and functional programming, which allows developers to better organize code and make it easier to understand and modify. At the same time, Rust's modular design allows different developers to work on the project at the same time without interfering with each other.
  4. Cross-platform: Rust widely supports multiple operating systems and platforms, including Windows, macOS, Linux, and Android, which enables developers to design applications for different platforms and devices.

How to write a desktop application in Rust

1. Install the Rust language toolchain: First, make sure you have installed the Rust programming language toolchain, including the Rust compiler (rustc) and package management tool (cargo). It can be installed by visiting the official Rust website and following the instructions.

2. Install the Rust plugin: Open IntelliJ IDEA, then go to "File" > "Settings" (Windows/Linux) or "IntelliJ IDEA" > "Preferences" (macOS). In the settings window, select "Plugins", search for "Rust", and click "Install". After the installation is complete, restart IntelliJ IDEA.

3. Create a new Rust project: In IntelliJ IDEA, select "File" > "New" > "Project", then select "Rust" in the project type list. Follow the prompts to configure the project settings, then click Finish to create the new project.

4. Add GUI library: You can choose a GUI library (such as GTK or Qt), and add the corresponding dependencies in the project's Cargo.toml file. For example, for GTK, the following dependencies need to be added:

5.[dependencies] gtk = "0.9"

6. Write code: Use the IntelliJ IDEA editor to write Rust code. Rust plugins provide code completion, syntax highlighting, error prompts, and more.

7. Build and run: Use IntelliJ IDEA's built-in terminal or an external terminal to run the cargo build and cargo run commands to build and run your Rust desktop application.    

Trial and Error and Verification of Nanny Tutorial

In order to verify whether this process is nanny-level enough, I sent it to my colleagues to operate it in advance, and finally reported an error in  cargo run  that there is no glib-2.0. This is because running a Rust project that includes gtk dependencies on macOS requires GTK+ and related libraries to be installed on the system first.  

This can be solved by using the Homebrew package manager. Follow the steps below:

  1. If Homebrew is not installed yet, visit the official Homebrew website and follow the instructions to install it.
  2. Open a terminal and run the following commands to install GTK+ and its dependencies:
  3. brew install gtk+3 brew install adwaita-icon-theme
  4. Install the pkg-config tool:
  5. brew install pkg-config
  6. Set environment variables to enable the Rust compiler and linker to find GTK+ libraries. In the shell configuration file (such as ~/.bashrc , ~/.zshrc , ~/.zshrc, etc.), add the following lines:   
  7. export PKG_CONFIG_PATH="/usr/local/opt/libffi/lib/pkgconfig"
  8. Save the changes and reload the shell configuration file for the changes to take effect. For example, if you modify ~/.zshrc , you should run the following command: 
  9. source ~/.zshrc

 

An example of desktop application effects written in Rust

When you see this hello world, you are more than half successful. Because there are already big guys who have open sourced a set of
https://github.com/lencx/ChatGPT and currently have more than 3w stars. It is a pity that due to theft and potential legal risks, subsequent updates will not be open source, and everyone can also use their creativity based on this version. The image below provides an example of a convenient download.

 

Author: Wang Mengjin

Content source: JD Cloud developer community

{{o.name}}
{{m.name}}

Guess you like

Origin my.oschina.net/u/4090830/blog/8774253