前一段时间Python的类型注解写的实在太难受了,想起了之前看过的rust,重新捡起来看看,然后学到了AI时代的学习方法。
简单几步
- 首选当然是读基础的文档,Rust程序设计语言
- 看完不知道做啥,刷点题吧,找一位大佬的刷题仓库。
- vscode安装拓展Mintlify Writer,一个AI生成自动生成文档的插件。
- 看不懂就选中再按
Ctrl+.
。哪里不会点哪里!
看看效果
选中ListNode
、derive
、inline
、Box
、unwrap_or
都可以生成对应的说明。放到一起是有点混乱,Rust的docstring是markdown格式的,可以很方便摘抄出来。
common/src/lib.rs
// Definition for singly-linked list.
pub mod structs {
/**
Defines a struct named ListNode with two fields, val of type i32 and next of type
Option<Box<ListNode>>.
Properties:
* `val`: The `val` property of the `ListNode` struct represents the value stored in the node. In
this case, it is an integer value of type `i32`.
* `next`: `next` is a property of the `ListNode` struct that represents a pointer to the next node
in a linked list. It is an `Option` type that can either be `None` if there is no next node, or
`Some` containing a `Box` that points to the next
`#[derive(PartialEq,Eq,Debug)]` is a Rust attribute that automatically generates implementations
of the `PartialEq`, `Eq`, and `Debug` traits for the `ListNode` struct. This allows instances of
the `ListNode` struct to be compared for equality using the `==` operator, and printed using the
`println!` macro with the `{:?}` format specifier.
*/
#[derive(PartialEq,Eq,Debug)]
pub struct ListNode{
pub val: i32,
pub next: Option<Box<ListNode>>,
}
impl ListNode{
/**
`#[inline]` is a Rust attribute that suggests the compiler to inline the function at the
call site. This means that instead of calling the function, the code of the function is
inserted directly into the calling code. This can improve performance by reducing the
overhead of function calls.
*/
#[inline]
pub fn new(val: i32) -> Self {
ListNode { next: None, val}
}
}
}
#[cfg(test)]
mod tests {
use super::structs::*;
#[test]
/**
`Box` is a smart pointer in Rust that provides **heap** allocation of
values. In this code, `Box` is used to create a new instance of
`ListNode` on the heap and store a pointer to it in the `next` field
of another `ListNode`. This allows for the creation of a linked list
data structure where each node is allocated on the heap and connected
to the next node through a pointer.
*/
/**
`unwrap_or` is a method provided by the `Option` type in Rust. It
takes a default value as an argument and returns either the value
contained in the `Option` if it is `Some`, or the default value if
the `Option` is `None`. In the given code,
`result.take().unwrap_or(Box::new(ListNode::new(0))).val` is used
to get the value of the `ListNode` contained in `result`, or a new
`ListNode` with a value of 0 if `result` is `None`. The `val`
property of the resulting `ListNode` is then returned.
*/
fn it_works() {
let mut result = Some(
Box::new(ListNode {
val: 9,
next: Some(Box::new(ListNode {
val: 8,
next: Some(Box::new(ListNode {
val: 3,
next: Some(Box::new(ListNode::new(3))),
})),
})),
}));
assert_eq!(result.take().unwrap_or(Box::new(ListNode::new(0))).val, 9);
}
}
插件有个问题是选中的内容是行内的话,生成注释会破坏原来的格式,需要手动调整下。
当然自动生成的内容基本只是解释现有代码, 不涉及更深层次的内容,还需要结合其他方式学习。
就这样~