提交 5b5c636a 编写于 作者: lean2's avatar lean2

change structure

上级 61964eb1
.DS_Store
\ No newline at end of file
.DS_Store
# Added by cargo
/target
# 2021 rCore夏令营
进度更新在notion中,以下是notion链接
[总记录]("https://www.notion.so/xuebling/rCore-bf1f5369c3f840aab22485415e55e0e6")
[第一周记录]("https://www.notion.so/xuebling/7-1-7-7-f3392fd94608422382f970b4b93914cf")
[第二周记录]("https://www.notion.so/xuebling/7-8-7-14-bef87401b4fc496399888317078f7dd2")
\ No newline at end of file
# 7月1日-7月7
学习内容: 做Rust编程语言练习题
虽然以前已经写过一些rust项目,但是做下练习也挺不错的,以下是对做练习时学到东西的一些总结
## 初级项目:
### rustlings
1. struct 更新语法,以前忽略了,很实用的语法糖
```bash
let your_order = Order{name: String::from("Hacker in Rust"), count: 1, ..order_template};
```
2. 宏可以定义多个语法表达式
3. 为集合创建迭代器通常有三种方法
```bash
iter(), which iterates over &T.
iter_mut(), which iterates over &mut T.
into_iter(), which iterates over T.
```
4. 自定义error只需要创建一个结构体,derive Debug trait,在impl Display和Error这两个trait就可以了
![rustling已完成](../pics/rustling_done.png)
### [32 Rust Quizes](https://dtolnay.github.io/rust-quiz/1)
## Rust中级练习题
1. 287 寻找重复数:
给定一个包含 n + 1 个整数的数组 nums ,其数字都在 1 到 n 之间(包括 1 和 n),可知至少存在一个重复的整数。
假设 nums 只有 一个重复的整数 ,找出 这个重复的数 。
你设计的解决方案必须不修改数组 nums 且只用常量级 O(1) 的额外空间。
```rust
impl Solution {
//转化成有环链表寻找环的起点的问题
pub fn find_duplicate(nums: Vec<i32>) -> i32 {
let mut s = 0;
let mut f = 0;
loop {
s = nums[s as usize];
f = nums[nums[f as usize] as usize];
if s == f { break; }
}
s = 0;
while s != f {
s = nums[s as usize];
f = nums[f as usize];
}
s
}
}
```
```java
class Solution {
public int findDuplicate(int[] nums) {
int slow = 0, fast = 0;
do {
slow = nums[slow];
fast = nums[nums[fast]];
} while (slow != fast);
slow = 0;
while (slow != fast) {
slow = nums[slow];
fast = nums[fast];
}
return slow;
}
}
```
总结:
- 这道题没想明白,看了题解做出来的,题目还是挺有意思的,竟然能够转化为链表的问题。
- rust数组不能使用i32作为索引,在刷题时总是有点不方便。
- 模式识别:以后遇到给一个数组A[i,j],数组内的数均 i<x<j 的这种情况,就可以考虑从链表的角度去解决问题了,
- 这道题也可以用二分法和位运算去做
## 本周总结
这周事情有点多,所以只把rustling给做了,外加做了一道算法题,下周要把rust练习题全部搞定,然后剩下两周来做rCore实验。
\ No newline at end of file
此差异已折叠。
[build]
target = "riscv64imac-unknown-none-elf"
[target.riscv64imac-unknown-none-elf]
rustflags = [
"-Clink-arg=-Tsrc/linker.ld", "-Cforce-frame-pointers=yes"
]
\ No newline at end of file
[package]
name = "os"
version = "0.1.0"
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
[profile.dev]
panic = "abort"
# panic 后直接调用panic_handler处理,禁止堆栈展开
[profile.release]
panic = "abort"
\ No newline at end of file
TARGET := riscv64imac-unknown-none-elf
MODE := debug
KERNEL_FILE := target/$(TARGET)/$(MODE)/os
BIN_FILE := target/$(TARGET)/$(MODE)/os.bin
BASE_ADDRESS:= 0x80200000
#BOOTLOADER := ../bootloader/rustsbi-qemu.bin
BOOTLOADER := default
OBJDUMP := rust-objdump --arch-name=riscv64
OBJCOPY := rust-objcopy --binary-architecture=riscv64
.PHONY: doc kernel build clean qemu run
# 默认 build 为输出二进制文件
build: $(BIN_FILE)
# 通过 Rust 文件中的注释生成 kernel 的文档
doc:
@cargo doc --document-private-items
# 编译 kernel
kernel:
@cargo build
# 生成 kernel 的二进制文件
$(BIN_FILE): kernel
@$(OBJCOPY) $(KERNEL_FILE) --strip-all -O binary $@
# 查看反汇编结果
asm:
@$(OBJDUMP) -d $(KERNEL_FILE) | less
# 清理编译出的文件
clean:
@cargo clean
# 运行 QEMU
qemu: build
@qemu-system-riscv64 \
-machine virt \
-nographic \
-bios $(BOOTLOADER) \
-device loader,file=$(BIN_FILE),addr=$(BASE_ADDRESS)
debug: build
@tmux new-session -d \
"qemu-system-riscv64 -machine virt -nographic -bios $(BOOTLOADER) -device loader,file=$(BIN_FILE),addr=$(BASE_ADDRESS) -s -S" && \
tmux split-window -h "riscv64-unknown-elf-gdb -ex 'file $(KERNEL_FILE)' -ex 'set arch riscv:rv64' -ex 'target remote localhost:1234'" && \
tmux -2 attach-session -d
# 一键运行
run: qemu
\ No newline at end of file
# 作用是用来装载操作系统,也就是
# 但是这个汇编文件是如何获取到linker.ld中声明的_start的呢
# linker.ld 该文件的关系是什么
# 我们在 linker.ld 中将程序入口设置为了 _start,因此在这里我们将填充这个标签
.section .text.entry
.globl _start
_start:
# 写入栈空间
la sp, boot_stack_top
# 调用操作系统主函数
call rust_main
# 回忆:bss 段是 ELF 文件中只记录长度,而全部初始化为 0 的一段内存空间
# 这里声明字段 .bss.stack 作为操作系统启动时的栈
.section .bss.stack
.global boot_stack
boot_stack:
# 16K 启动栈大小
.space 4096 * 16
.global boot_stack_top
boot_stack_top:
# 栈结尾
\ No newline at end of file
/* 作用是指导编译器设置内存布局,设置各个段的位置 */
/* 目标架构 */
OUTPUT_ARCH(riscv)
/* 执行入口,因此编译器会将_start这个函数作为程序的入口,而_start会调用rustmain,从而启动操作系统*/
ENTRY(_start)
/* 数据存放起始地址 */
BASE_ADDRESS = 0x80200000;
SECTIONS
{
/* . 表示当前地址(location counter) */
. = BASE_ADDRESS;
/* start 符号表示全部的开始位置 */
kernel_start = .;
text_start = .;
.text : {
/* 该段是在entry.asm中声明的 */
*(.text.entry)
*(.text .text.*)
}
rodata_start = .;
.rodata : {
*(.rodata .rodata.*)
}
data_start = .;
.data : {
*(.data .data.*)
}
bss_start = .;
.bss : {
*(.sbss .bss .bss.*)
}
kernel_end = .;
}
\ No newline at end of file
#![no_std]
#![no_main]
#![feature(global_asm)]
#![feature(llvm_asm)]
use core::panic::PanicInfo;
global_asm!(include_str!("entry.asm"));
pub fn console_putchar(ch: u8) {
let _ret: usize;
let arg0: usize = ch as usize;
let arg1: usize = 0;
let arg2: usize = 0;
let which: usize = 1;
unsafe {
llvm_asm!("ecall"
: "={x10}" (_ret)
: "{x10}" (arg0), "{x11}" (arg1), "{x12}" (arg2), "{x17}" (which)
: "memory"
: "volatile"
);
}
}
#[no_mangle]
pub extern "C" fn rust_main() -> !{
loop{}
}
#[panic_handler]
fn panic(info: &PanicInfo) -> !{
loop {}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册