0.1. Rust
https://rustcc.cn/ https://crates.io/ https://doc.rust-lang.org/cargo/reference/manifest.html https://doc.rust-lang.org/book/title-page.html https://rustwasm.github.io/docs/book/introduction.html https://rust-lang-nursery.github.io/rust-cookbook/intro.html https://actix.rs/ https://doc.rust-lang.org/book/ch00-00-introduction.html https://rust-lang.budshome.com/ https://play.rust-lang.org/ https://doc.rust-lang.org/std/index.html https://slint-ui.com/releases/1.0.0/docs/rust/slint/ https://slint-ui.com/releases/1.0.0/docs/slint/src/concepts/file.html https://doc.rust-lang.org/cargo/ https://dhghomon.github.io/easy_rust/Chapter_44.html https://www.cnblogs.com/andy-chi/p/16804923.html 迭代器
附:Derivable Traits derive 会自动生成一些代码,这些代码自动实现了一些标准库的trait。
Debug 调试的时候需要打印一些东西,使用format或者println,占位符{:?}来直接打印对象。
PartialEq and Eq for Equality Comparisons
PartialOrd and Ord for Ordering Comparisons Clone and Copy for Duplicating Values Hash for Mapping a Value to a Value of Fixed Size Default for Default Values
附:Operators
Operator Example Explanation Overloadable? ! ident!(…), ident!{…}, ident![…] Macro expansion ! !expr Bitwise or logical complement Not != var != expr Nonequality comparison PartialEq % expr % expr Arithmetic remainder Rem %= var %= expr Arithmetic remainder and assignment RemAssign & &expr, &mut expr Borrow & &type, &mut type, &‘a type, &‘a mut type Borrowed pointer type & expr & expr Bitwise AND BitAnd &= var &= expr Bitwise AND and assignment BitAndAssign && expr && expr Short-circuiting logical AND
- expr * expr Arithmetic multiplication Mul *= var *= expr Arithmetic multiplication and assignment MulAssign
- *expr Dereference Deref
- *const type, *mut type Raw pointer
- trait + trait, ‘a + trait Compound type constraint
- expr + expr Arithmetic addition Add += var += expr Arithmetic addition and assignment AddAssign , expr, expr Argument and element separator
-
- expr Arithmetic negation Neg
- expr - expr Arithmetic subtraction Sub -= var -= expr Arithmetic subtraction and assignment SubAssign → fn(…) → type, |…| → type Function and closure return type . expr.ident Member access .. .., expr.., ..expr, expr..expr Right-exclusive range literal PartialOrd ..= ..=expr, expr..=expr Right-inclusive range literal PartialOrd .. ..expr Struct literal update syntax .. variant(x, ..), struct_type { x, .. } “And the rest” pattern binding … expr…expr (Deprecated, use ..= instead) In a pattern: inclusive range pattern / expr / expr Arithmetic division Div /= var /= expr Arithmetic division and assignment DivAssign : pat: type, ident: type Constraints : ident: expr Struct field initializer : ‘a: loop {…} Loop label ; expr; Statement and item terminator ; […; len] Part of fixed-size array syntax << expr << expr Left-shift Shl <⇐ var <⇐ expr Left-shift and assignment ShlAssign < expr < expr Less than comparison PartialOrd ⇐ expr ⇐ expr Less than or equal to comparison PartialOrd = var = expr, ident = type Assignment/equivalence expr expr Equality comparison PartialEq ⇒ pat ⇒ expr Part of match arm syntax
expr > expr Greater than comparison PartialOrd = expr >= expr Greater than or equal to comparison PartialOrd
expr >> expr Right-shift Shr = var >>= expr Right-shift and assignment ShrAssign @ ident @ pat Pattern binding ^ expr ^ expr Bitwise exclusive OR BitXor ^= var ^= expr Bitwise exclusive OR and assignment BitXorAssign | pat | pat Pattern alternatives | expr | expr Bitwise OR BitOr |= var |= expr Bitwise OR and assignment BitOrAssign || expr || expr Short-circuiting logical OR ? expr? Error propagation
附:Non-operator Symbols
The following list contains all non-letters that don’t function as operators; that is, they don’t behave like a function or method call. Table B-2 shows symbols that appear on their own and are valid in a variety of locations. Symbol Explanation ‘ident Named lifetime or loop label …u8, …i32, …f64, …usize, etc. Numeric literal of specific type ”…” String literal r”…”, r#”…”#, r##”…”##, etc. Raw string literal, escape characters not processed b”…” Byte string literal; constructs a [u8] instead of a string br”…”, br#”…”#, br##”…”##, etc. Raw byte string literal, combination of raw and byte string literal ’…’ Character literal b’…’ ASCII byte literal |…| expr Closure ! Always empty bottom type for diverging functions _ “Ignored” pattern binding; also used to make integer literals readable
Table B-3 shows symbols that appear in the context of a path through the module hierarchy to an item.
Symbol Explanation
ident::ident Namespace path
::path Path relative to the crate root (i.e., an explicitly absolute path)
self::path Path relative to the current module (i.e., an explicitly relative path).
super::path Path relative to the parent of the current module
type==ident,
Table B-4 shows symbols that appear in the context of using generic type parameters.
Symbol Explanation
path<…> Specifies parameters to generic type in a type (e.g., Vec
Table B-5 shows symbols that appear in the context of constraining generic type parameters with trait bounds.
Symbol Explanation T: U Generic parameter T constrained to types that implement U T: ‘a Generic type T must outlive lifetime ‘a (meaning the type cannot transitively contain any references with lifetimes shorter than ‘a) T: ‘static Generic type T contains no borrowed references other than ‘static ones ‘b: ‘a Generic lifetime ‘b must outlive lifetime ‘a T: ?Sized Allow generic type parameter to be a dynamically sized type ‘a + trait, trait + trait Compound type constraint
Table B-6 shows symbols that appear in the context of calling or defining macros and specifying attributes on an item.
Symbol Explanation #[meta] Outer attribute #![meta] Inner attribute ident:kind Macro capture $(…)… Macro repetition ident!(…), ident!{…}, ident![…] Macro invocation
Table B-7 shows symbols that create comments.
Symbol Explanation // Line comment //! Inner line doc comment /// Outer line doc comment /…/ Block comment /!…/ Inner block doc comment /**…*/ Outer block doc comment
Table B-8 shows symbols that appear in the context of using tuples. Symbol Explanation () Empty tuple (aka unit), both literal and type (expr) Parenthesized expression (expr,) Single-element tuple expression (type,) Single-element tuple type (expr, …) Tuple expression (type, …) Tuple type expr(expr, …) Function call expression; also used to initialize tuple structs and tuple enum variants expr.0, expr.1, etc. Tuple indexing
Table B-9 shows the contexts in which curly braces are used.
Context Explanation {…} Block expression Type {…} struct literal
Table B-10 shows the contexts in which square brackets are used.
Context Explanation […] Array literal [expr; len] Array literal containing len copies of expr [type; len] Array type containing len instances of type expr[expr] Collection indexing. Overloadable (Index, IndexMut) expr[..], expr[a..], expr[..b], expr[a..b] Collection indexing pretending to be collection slicing, using Range, RangeFrom, RangeTo, or RangeFull as the “index”
附:术语 ! Statement
是执行某些操作,没有返回值的步骤。
let x = 123;
! Expression
表达式有计算步骤且有返回值。
a = 7
b + 2
c * (a + b)
! 表达式块
Rust 中可以在一个用 {} 包括的块里编写一个较为复杂的表达式。在块中可以使用函数语句,最后一个步骤是Expression,此表达式的结果值是整个表达式块所代表的值。这种表达式块叫做函数体表达式。注意:x + 1 之后没有分号,否则它将变成一条 Statement!
fn main() {
let x = 5;
let y = {
let x = 3;
x + 1
};
println!("x 的值为 : {}", x);
println!("y 的值为 : {}", y);
}
语句和表达式
语句(Statements):执行语句不返回结果 表达式(Expressions):计算并返回结果,表达式可以是语句的一部分。
/// Statement
let y = 6;
/// Error, 后面的 let statement 不返回任何值
let x = (let y = 6);
let x = {
/// Statement
let z = 3;
/// Expression
z + 1
}
/// 块表达式
{
let x = 3;
x + 1
}
/// 这是一个语句,不是表达式
x+1;
附:符号
- 可以在文字后面紧跟着类型。100u32
- 处理比较长的数字的时候可以使用下划线的方式书写。 1_000_000u32
! +
// 整数加法
println!("1 + 2 = {}", 1u32 + 2)
! -
// 注意i32,如果使用u32会出现overflow问题。
println!("1 - 2 = {}", 1i32 - 2);
! ..
x..y Range 表示 [x, y) 的数学含义,含左不含右。(0..5).rev() 倒叙序列。
x..=y RangeInclusive 表示[x,y]的数学含义,含左含右。
.. RangeFull // TODO有什么用途?
..y RangeTo 等价于 0..y
x.. RangeFrom 等价于位置 x 到数据结束
! 与或非
println!("true AND false is {}", true && false);
println!("true OR false is {}", true || false);
println!("NOT true is {}", !true);
! 位运算 & | ^
fn main() {
println!("0011 AND 0101 is {:04b}", 0b0011u32 & 0b0101);
println!("0011 OR 0101 is {:04b}", 0b0011u32 | 0b0101);
println!("0011 XOR 0101 is {:04b}", 0b0011u32 ^ 0b0101);
println!("1 << 5 is {}", 1u32 << 5);
println!("0x80 >> 2 is 0x{:x}", 0x80u32 >> 2);
}
! ?
方法返回值类型是Result,可以在主体内直接使用?将所调用的方法的Error对象直接返回给上级,注意调用者的返回类型也需要与和Error匹配。
9 | | let n = stream.read(&mut buffer[..])?;
| | ^ cannot use the `?` operator in a function that returns `()`
slint https://slint-ui.com 官方网站 https://docs.rs/slint/1.0.0/slint/index.html 包 https://slint-ui.com/releases/1.0.0/docs/tutorial/rust/introduction.html 示例程序 https://slint-ui.com/releases/1.0.0/editor/ 在线调试UI https://github.com/slint-ui/slint-rust-template 测试项目 https://slint-ui.com/docs/rust/slint/ https://slint-ui.com/releases/1.0.0/docs/slint/src/concepts/intro.html https://slint.dev/releases/1.1.0/docs/slint
/Cargo.toml [package] name = “my-slint-rust-template” version = “0.1.0” authors = [“杜超群 [email protected]”] edition = “2021” build = “build.rs”
1. See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
引入依赖 [dependencies] slint = “1.1.0”
[build-dependencies] slint-build = “1.1.0”
/build.rs 指定UI文件位置 fn main() { slint_build::compile(“ui/appwindow.slint”).unwrap(); }
/ui/appwindow.slint 绘制UI import { Button, VerticalBox } from “std-widgets.slint”;
export component AppWindow inherits Window {
in-out property
/scr/main.rs 主程序 slint::include_modules!();
fn main() → Result<(), slint::PlatformError> { let ui = AppWindow::new()?;
// 使用weak引用处理每个绑定函数 let ui_weak = ui.as_weak();
ui.on_request_increase_value(move || { let ui = ui_weak.unwrap(); ui.set_counter(ui.get_counter() + 1); });
// 使用weak引用处理每个绑定 let ui_weak = ui.as_weak(); ui.on_button1_pressed(move || { let ui: AppWindow = ui_weak.upgrade().unwrap(); let mut value = ui.get_counter(); value = value + 1; ui.set_counter(value); });
ui.on_button2_pressed(|| { println!(“todo”); });
ui.run() }
InputText