rust编程环境

windows10 + mingw64

安装mingw-w64

mingw-w64也是C/C++编译器,就是 GCC 的 Windows 版本 。

打开 https://sourceforge.net/projects/mingw-w64/files/ ,选择x86_64-win32-seh,大约50来M,这里下载得到 x86_64-8.1.0-release-win32-seh-rt_v6-rev0.7z

版本选择,可参考: MinGW-w64 版本选择方法 – 页岩 – 博客园

解压后复制到 D:\mingw64, 将 D:\mingw64\bin 加入windows用户path

打开新的cmd终端,输入 gcc -v 验证安装

安装rustup

官网: https://www.rust-lang.org/tools/install , 下载64位

官方下载器下载安装的rust主要有两个文件夹,而且会默认生成到C盘下。 但是,我们通过修改环境变量可以自定义安装路径。

创建目录 d:\rust\, 复制下载的 rustup-init.exe 到该目录

创建目录 d:\rust\.cargo, d:\rust\.rustup, 对应创建用户环境变量 CARGO_HOME,RUSTUP_HOME, 并将 %CARGO_HOME%,%RUSTUP_HOME% 加入用户path

命令行下运行 rustup-init.exe

D:\rust>set http_proxy=socks5://127.0.0.1:1080

D:\rust>set https_proxy=socks5://127.0.0.1:1080

D:\rust>rustup-init.exe

Rust Visual C++ prerequisites

Rust requires a linker and Windows API libraries but they don't seem to be
available.

These components can be acquired through a Visual Studio installer.

1) Quick install via the Visual Studio Community installer
   (free for individuals, academic uses, and open source).

2) Manually install the prerequisites
   (for enterprise and advanced users).

3) Don't install the prerequisites
   (if you're targeting the GNU ABI).

>2


You can acquire the build tools by installing Microsoft Visual Studio.

  https://visualstudio.microsoft.com/downloads/

Check the box for "Desktop development with C++" which will ensure that the
needed components are installed. If your locale language is not English,
then additionally check the box for English under Language packs.

For more details see:

  https://rust-lang.github.io/rustup/installation/windows-msvc.html

Install the C++ build tools before proceeding.

If you will be targeting the GNU ABI or otherwise know what you are
doing then it is fine to continue installation without the build
tools, but otherwise, install the C++ build tools before proceeding.

Continue? (y/N)
y


Welcome to Rust!

This will download and install the official compiler for the Rust
programming language, and its package manager, Cargo.

Rustup metadata and toolchains will be installed into the Rustup
home directory, located at:

  d:\rust\.rustup

This can be modified with the RUSTUP_HOME environment variable.

The Cargo home directory is located at:

  d:\rust\.cargo

This can be modified with the CARGO_HOME environment variable.

The cargo, rustc, rustup and other commands will be added to
Cargo's bin directory, located at:

  d:\rust\.cargo\bin

This path will then be added to your PATH environment variable by
modifying the HKEY_CURRENT_USER/Environment/PATH registry key.

You can uninstall at any time with rustup self uninstall and
these changes will be reverted.

Current installation options:


   default host triple: x86_64-pc-windows-msvc
     default toolchain: stable (default)
               profile: default
  modify PATH variable: yes

1) Proceed with installation (default)
2) Customize installation
3) Cancel installation
>2

I'm going to ask you the value of each of these installation options.
You may simply press the Enter key to leave unchanged.

Default host triple? [x86_64-pc-windows-msvc]
x86_64-pc-windows-gnu

Default toolchain? (stable/beta/nightly/none) [stable]


Profile (which tools and data to install)? (minimal/default/complete) [default]
complete

Modify PATH variable? (Y/n)
Y


Current installation options:


   default host triple: x86_64-pc-windows-gnu
     default toolchain: stable
               profile: complete
  modify PATH variable: yes

1) Proceed with installation (default)
2) Customize installation
3) Cancel installation
>1

info: profile set to 'complete'
info: setting default host triple to x86_64-pc-windows-gnu
info: syncing channel updates for 'stable-x86_64-pc-windows-gnu'
732.1 KiB / 732.1 KiB (100 %) 122.7 KiB/s in  4s ETA:  0s
info: latest update on 2023-06-01, rust version 1.70.0 (90c541806 2023-05-31)
warning: Force-skipping unavailable component 'miri-x86_64-pc-windows-gnu'
info: downloading component 'cargo'
  8.2 MiB /   8.2 MiB (100 %)   1.3 MiB/s in  7s ETA:  0s
info: downloading component 'clippy'
  3.4 MiB /   3.4 MiB (100 %)   1.3 MiB/s in  2s ETA:  0s
info: downloading component 'llvm-tools'
 69.2 MiB /  69.2 MiB (100 %)   1.2 MiB/s in  1m  9s ETA:  0s
info: downloading component 'rls'
info: downloading component 'rust-analysis'
info: downloading component 'rust-analyzer'
  9.6 MiB /   9.6 MiB (100 %)   1.0 MiB/s in 12s ETA:  0s
info: downloading component 'rust-docs'
 13.5 MiB /  13.5 MiB (100 %)   1.2 MiB/s in 11s ETA:  0s
info: downloading component 'rust-mingw'
  4.7 MiB /   4.7 MiB (100 %)   1.0 MiB/s in  5s ETA:  0s
info: downloading component 'rust-src'
  2.3 MiB /   2.3 MiB (100 %) 673.6 KiB/s in  4s ETA:  0s
info: downloading component 'rust-std'
 25.5 MiB /  25.5 MiB (100 %)   1.2 MiB/s in 24s ETA:  0s
info: downloading component 'rustc'
 72.8 MiB /  72.8 MiB (100 %)   1.3 MiB/s in  1m 11s ETA:  0s
info: downloading component 'rustfmt'
  2.9 MiB /   2.9 MiB (100 %)   1.0 MiB/s in  3s ETA:  0s
info: installing component 'cargo'
info: installing component 'clippy'
info: installing component 'llvm-tools'
 69.2 MiB /  69.2 MiB (100 %)  10.1 MiB/s in  6s ETA:  0s
info: installing component 'rls'
info: installing component 'rust-analysis'
info: installing component 'rust-analyzer'
info: installing component 'rust-docs'
 13.5 MiB /  13.5 MiB (100 %)   1.7 MiB/s in  7s ETA:  0s
info: installing component 'rust-mingw'
info: installing component 'rust-src'
info: installing component 'rust-std'
 25.5 MiB /  25.5 MiB (100 %)   9.6 MiB/s in  4s ETA:  0s
info: installing component 'rustc'
 72.8 MiB /  72.8 MiB (100 %)  10.4 MiB/s in  6s ETA:  0s
info: installing component 'rustfmt'
info: default toolchain set to 'stable-x86_64-pc-windows-gnu'

  stable-x86_64-pc-windows-gnu installed - rustc 1.70.0 (90c541806 2023-05-31)


Rust is installed now. Great!

To get started you may need to restart your current shell.
This would reload its PATH environment variable to include
Cargo's bin directory (d:\rust\.cargo\bin).

Press the Enter key to continue.  profile: complete
  modify PATH variable: yes

1) Proceed with installation (default)
2) Customize installation
3) Cancel installation
>1

打开新的cmd终端,

C:\WINDOWS\system32>rustc -V
rustc 1.70.0 (90c541806 2023-05-31)

设置 visual studio code

安装插件:rust-analyzer, rust

创建文件夹 d:\rustProjects

通常,对于 type-level 的构造 Rust 倾向于使用驼峰命名法,而对于 value-level 的构造使用蛇形命名法

cargo new rust_test 创建新项目

D:\rustProjects>cargo new rust_test
     Created binary (application) `rust_test` package

在vscode打开这个`rust_tes文件夹,打开src下的main.rs文件,在VSCODE点击运行,启动调式.

python中调用 rust:PyO3

PyO3是python和rust语言之间的桥梁,它允许你从python中调用rust,也运行rust中调用python,所以用PyO3可以创造一个高性能的python扩展,这个扩展可以直接用rust来编写。

查看pyo3版本 :Releases · PyO3/pyo3 · GitHub ,比如: 0.19.1

添加用户环境变量 PYO3_PYTHON, 值为:D:\miniconda3\python.exe

Cargo.toml内容如下, pyo3 依赖的版本和下载版本一致:

[package]
name = "rust_test"
version = "0.1.0"
edition = "2021"


[lib]
name = "rust"
crate-type = ["cdylib"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies.pyo3]
version = "0.19.1"
features = ["extension-module"]

main.rs重命名为 lib.rs ,内容:

//引入pyo3 prelude
use pyo3::prelude::*; 

//注释函数#[pyfunction]以将其转换为 PyCFunction
//将结果包装在一个 PyResult
#[pyfunction]
fn multiply(a: isize, b: isize) -> PyResult<isize> {
    Ok(a * b)
}

//将函数添加到 #[pymodule]
//在一个名为rust(在最后一个函数的名称之后)的Python 模块中公开乘法函数。
//为方便起见,请确保 Cargo.toml 中库的名称与使用#[pymodule].
//将以下内容放入Cargo.toml
// 当这两个名称匹配时,maturin构建工具将使用该名称将 Rust 库安装为 Python 模块。
//[lib]
//name = "rust"

#[pymodule]
fn rust(_py: Python, m: &PyModule) -> PyResult<()> {
    m.add_function(wrap_pyfunction!(multiply, m)?)?;
    Ok(())
}

编译方式1:编译为dll

编译: D:\rustProjects\rust_test> cargo build –release

得到 D:\rustProjects\rust_test\target\release\rust.dll

将rust.dll 重命名为 rust.pyd , 可以拷贝到任意目录、任意conda环境使用,如拷贝到

D:\rustProjects\rust_test\rust.pyd, 在base环境中使用

D:\rustProjects\rust_test>activate base

(base) D:\rustProjects\rust_test>python
Python 3.10.12 | packaged by Anaconda, Inc. | (main, Jul  5 2023, 19:01:18) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import rust
>>> result = rust.multiply(2, 3)
>>> print(result)
6

编译方式2:使用maturin

更简单的方式: 使用maturin,该工具将为您编译 Rust 代码,并将编译后的代码作为 Python 模块安装在您的虚拟环境中

D:\rustProjects>conda create -n temp
Collecting package metadata (current_repodata.json): done
Solving environment: done

## Package Plan ##

  environment location: d:\miniconda3\envs\temp



Proceed ([y]/n)? y

Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use
#
#     $ conda activate temp
#
# To deactivate an active environment, use
#
#     $ conda deactivate


D:\rustProjects>conda activate temp

(temp) D:\rustProjects>conda install maturin

编译安装:

(temp) D:\rustProjects>cd rust_test

(temp) D:\rustProjects\rust_test>maturin develop
  Downloaded bitflags v1.3.2
  Downloaded windows_x86_64_gnullvm v0.48.0
  Downloaded redox_syscall v0.3.5
  Downloaded windows_aarch64_msvc v0.48.0
  Downloaded windows_i686_gnu v0.48.0
  Downloaded windows_i686_msvc v0.48.0
  Downloaded windows_x86_64_msvc v0.48.0
  Downloaded windows_aarch64_gnullvm v0.48.0
  Downloaded 8 crates (3.6 MB) in 2m 06s
:link: Found pyo3 bindings
:snake: Found CPython 3.11 at d:\miniconda3\envs\temp\python.exe
   Compiling windows_x86_64_gnu v0.48.0
   Compiling cfg-if v1.0.0
   Compiling pyo3-build-config v0.19.1
   Compiling scopeguard v1.1.0
   Compiling smallvec v1.11.0
   Compiling libc v0.2.147
   Compiling memoffset v0.9.0
   Compiling unindent v0.1.11
   Compiling windows-targets v0.48.1
   Compiling lock_api v0.4.10
   Compiling parking_lot_core v0.9.8
   Compiling parking_lot v0.12.1
   Compiling pyo3-ffi v0.19.1
   Compiling pyo3 v0.19.1
   Compiling rust_test v0.1.0 (D:\rustProjects\rust_test)
    Finished dev [unoptimized + debuginfo] target(s) in 16.10s
:package: Built wheel for CPython 3.11 to C:\Users\dfqn\AppData\Local\Temp\.tmpyqlh72\rust_test-0.1.0-cp311-none-win_amd64.whl
:hammer_and_wrench: Installed rust_test-0.1.0

安装的rust 只能在temp环境中调用:

C:\>activate temp

(temp) C:\>python
Python 3.11.4 | packaged by Anaconda, Inc. | (main, Jul  5 2023, 13:47:18) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import rust
>>>
>>> result = rust.multiply(2, 3)
>>> print(result)
6
>>>

使用国内源

参考: http://rsproxy.cn/ 设置 ~/.cargo/config,

添加系统变量:

RUSTUP_DIST_SERVER="https://rsproxy.cn"

RUSTUP_UPDATE_ROOT="https://rsproxy.cn/rustup"

需要安装git: Git – Downloading Package

卸载

rustup self uninstall

正文完
 0
评论(没有评论)