您的位置:首页 > 文旅 > 旅游 > 免费psd素材网_网页设计实训报告范文免费_搭建网站步骤_seo优化在线诊断

免费psd素材网_网页设计实训报告范文免费_搭建网站步骤_seo优化在线诊断

2025/4/2 1:33:23 来源:https://blog.csdn.net/qq_63401240/article/details/146489094  浏览:    关键词:免费psd素材网_网页设计实训报告范文免费_搭建网站步骤_seo优化在线诊断
免费psd素材网_网页设计实训报告范文免费_搭建网站步骤_seo优化在线诊断

目录

前言

正文

toml文件的基础

注释——# Comment

键值对——Key/Value

表——[table]

内联表——Inline Table

数组——Array

package和crate

Cargo.toml文件

Cargo.toml——dependencies

Cargo.toml——lib

crate-type

main.rs


前言

【Tauri2】001——安装及运行-CSDN博客https://blog.csdn.net/qq_63401240/article/details/146486117?spm=1001.2014.3001.5501

前面介绍了安装和运行,

笔者使用快速安装的项目start

接下来进入src-tauri目录下的Cargo.toml

正文

toml文件的基础

参考

TOML: Tom's Obvious Minimal Languagehttps://toml.io/en/

在Cargo.toml文件中,主要有下面这些

注释——# Comment

# 这是注释(Comment)
name = "Rust"  # 行内注释

键值对——Key/Value

name = "start" # name 是键,"start"是值

表——[table]

[package] # 表
name = "start" 

内联表——Inline Table

[dependencies] # table 
tauri = { version = "2", features = [] } # 内联表

数组——Array

crate-type = ["staticlib", "cdylib", "rlib"] # 数组

其他的类型可以参考文档

package和crate

src-tauri这个目录及其子文件,整体可以认为是个package

在Rust中的crate,有两种crate,binary crate 和library crate

从src的目录下,发现有lib.rs和main.rs

可以断言,同时有binary crate 和library crate


Cargo.toml文件

如下

[package]
name = "start"
version = "0.1.0"
description = "A Tauri App"
authors = ["you"]
edition = "2021"[lib]
name = "start_lib"
crate-type = ["staticlib", "cdylib", "rlib"][build-dependencies]
tauri-build = { version = "2", features = [] }[dependencies]
tauri = { version = "2", features = [] }
tauri-plugin-opener = "2"
serde = { version = "1", features = ["derive"] }
serde_json = "1"

Cargo.toml——dependencies

首先关注其中一个依赖

serde = { version = "1", features = ["derive"] }

 serde是键,它所对应的值是内联表,在内联表中有两个键

version对应的值表示使用版本1.x,可以写详细点。

features对应的值表示使用属性宏(Attribute Macro)——derive。

没有features,则表示,使用默认功能或者说默认特性(trate)

写了features,但features为空数组,则表示禁用所有特性

feature对应的值控制crate的特性或者宏的可用性。


Cargo.toml——lib

再关注一张表——lib


[lib]
name = "start_lib"
crate-type = ["staticlib", "cdylib", "rlib"]

 有个键name,值为start_lib

进入lib.rs文件。其中有个方法——run

#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {tauri::Builder::default().plugin(tauri_plugin_opener::init()).invoke_handler(tauri::generate_handler![greet]).run(tauri::generate_context!()).expect("error while running tauri application");
}

进入main.rs,其中main函数如下

fn main() {start_lib::run()
}

start_lib正是值,run正是lib.rs中的方法,如果修改一下,笔者猜测会报错,尝试一下

如果只把值start_lib改成start_lib1,在main.rs中有个报错

使用未声明的 crate 或模块 `start_lib` [E0433]

运行命令cargo build,报错如下

error[E0433]: failed to resolve: use of undeclared crate or module `start_lib`                                                                                  --> src\main.rs:5:5|
5 |     start_lib::run()|     ^^^^^^^^^ use of undeclared crate or module `start_lib`|
help: there is a crate or module with a similar name|
5 |     start_lib1::run()|     ~~~~~~~~~~For more information about this error, try `rustc --explain E0433`.

意思没有start_lib,要使用start_lib1。

看来没有问题

main.rs是启动文件,lib.rs的run方法是启动的关键函数,名字也比较重要。


crate-type

下面还有一个键crate-type,对应的值是一个数组,三个字符串staticlib,cdylib,rlib

crate-type用于指定 crate 输出类型的一个字段,定义了编译器将 crate 编译成什么类型的库文件

staticlib:编译成传统的静态库,即 .a 文件,能被其他语言(如 C 或 C++)链接和使用

cdylib:编译成动态链接库,即 .so 文件(在 Linux 上)、.dll 文件(在 Windows 上)或 .dylib 文件(在 macOS 上)。

rlib:编译成Rust 自己的库格式,即 .rlib 文件。


main.rs——入口文件

进入main.rs中,最上面的内容如下

// Prevents additional console window on Windows in release, DO NOT REMOVE!!
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

很明显,双斜杠表示注释,先把内容翻译一下,大致如下

在发布版本中防止在 Windows 上出现额外的控制台窗口,切勿删除!

根据翻译,可以明白,下面的代码可以实现打包发布后,在window中不会出现额外的控制台。

要明白下面的代码的意思,这就涉及到Rust中的条件编译

参考如下

Conditional compilation - The Rust Referencehttps://doc.rust-lang.org/reference/conditional-compilation.html#:~:text=Source%20code%20can%20be%20made%20conditionally%20compiled%20using,and%20cfg_attr%20attributes%20and%20the%20built-in%20cfg%20macro.什么是条件编译?笔者的理解很简单,意思是根据条件进行编译

说白了,“相当于在编译的时候使用了if ”

举个栗子

#[cfg(target_os = "windows")]
fn print_windows() {println!("windows tauri build");
}

这段代码的意思,如果是在windows下进行编译,下面的代码才会被编译

将这段代码放到main.rs中的main函数下,即

fn main() {print_windows();start_lib::run()
}#[cfg(target_os = "windows")]
fn print_windows() {println!("windows tauri build");
}

运行命令cargo run ,结果如下

 对于cfg_attr,语法如下

#[cfg_attr(condition, attribute)]

第一个参数是条件,第二个参数是属性

attr是attribute的简写,可以简单地认为,根据条件控制属性。对于代码是否被编译,这不能控制。

它和#[cfg]有本质的区别。

举个栗子

#[cfg_attr(target_os = "windows",allow(unused_variables))]
fn use_var() {let a="asdasda";println!("Hello, world!");
}

代码的意思——在window中,允许没有使用的变量

正常情况下,定义了但没有使用的变量会发出警告。

结果如下

如果去掉cfg_attr,结果如下

# 和#!有什么区别?

最关键的一点就是范围。#!作用的对象是全局

再看代码

#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

可以理解为

在不是调试模式的条件下(在发布模式),windows_subsystem 设为windows,全局配置。

windows_subsystem ,英文的意思是windows的子系统

其中有两个选择

windows:将程序标记为GUI程序,没有控制台

console:将程序标记为控制台程序。

现在意思就很简单了

调试模式下保留控制台,发布模式移除控制台。这就是这段代码的意思。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com