RUST
计算长方形面积
最简单的
fn main() {
let w=30;
let l=200;
println!("{}",w*l);
}
使用函数
fn main() {
let w=30;
let l=200;
println!("{}",area(w, l));
}
fn area(width:u32,length:u32) -> u32 {
width*length
}
函数+元组
fn main() {
let rect=(30,50);
println!("{}",area(rect));
}
fn area(dim:(u32,u32)) -> u32 {
dim.0*dim.1
}
函数+结构体
struct Recttangle{
width:u32,
length:u32
}
fn main() {
let rect=&Recttangle { width:30,length:50};
println!("{}",area(rect));
}
fn area(rect:&Recttangle) -> u32 {
rect.width*rect.length
}
- 此时函数只是借用了所有权,结束后所有权仍然在main
- 直接打印rect
- 不会默认disaply和Debug
#[derive(Debug)]
struct Recttangle{
width:u32,
length:u32
}
fn main() {
let rect=&Recttangle { width:30,length:50};
println!("{}",area(rect));
println!("{:?}",rect)
}
fn area(rect:&Recttangle) -> u32 {
rect.width*rect.length
}
struct2
方法
定义
---#[derive(Debug)]
struct Recttangle{
width:u32,
length:u32
}
impl Recttangle {
fn area(&self) -> u32 {
self.width*self.length
}
}
fn main() {
let rect=&Recttangle { width:30,length:50};
println!("{}",rect.area());
println!("{:?}",rect)
}
- 以上方法不可变
- 没有->的运算符
- rust有自动引用和自动解引用
关联函数
- impl不用把self作为第一个负向
- 通常用于构造器
- c/c++的静态函数
MySQL连接池
环境配置
windows
- 打开vs
- 打开属性
- 将mysql的include添加到C/C++->常规->附加包含目录
- 将mysql的lib添加到链接器->常规->附加库目录
- 将libmysql.lib添加到链接器->输入->附加依赖项(最后)
C API连接
class Connection
{
public:
//构造
Connection();
//析构
~Connection();
//连接
bool connect(
std::string ip,
unsigned short port,
std::string user,
std::string password,
std::string dbname
);
//更新
bool update(std::string sql);
//查询
MYSQL_RES* query(std::string sql);
private:
MYSQL* _conn;
};
构造
Connection::Connection()
{
_conn = mysql_init(nullptr);
}
- 初始化conn到数据库的指针
析构
Connection::~Connection()
{
if (_conn != nullptr) mysql_close(_conn);
}
- 如果不是空指针,则关闭连接
连接
bool Connection::connect(std::string ip, unsigned short port, std::string username, std::string password, std::string dbname)
{
MYSQL* p = mysql_real_connect(_conn, ip.c_str(), username.c_str(), password.c_str(), dbname.c_str(), port, nullptr, 0);
return p != nullptr;
}
更新/查询等sql语句
bool Connection::update(std::string sql)
{
if (mysql_query(_conn, sql.c_str()))
{
return false;
}
return true;
}
- 将std::string 在内部特化或者直接传sql语句
CSS
CSS 定位与 Flex 布局
position
五个值
static(默认)
- 按正常文档流排列,无法用 top/left 等偏移。
- z-index 不生效——因为 static 元素不参与层叠排序逻辑(相当于永远排在定位元素之下)。
relative
- 保留原空间(不脱离文档流),通过 top/right/bottom/left 相对自身原本位置偏移。
- 偏移后可能覆盖其他元素,此时 z-index 可调整层级。
.box {
position: relative;
top: 10px; /* 相对原位置向下偏移 10px */
left: 20px;
z-index: 5; /* 生效 */
}
absolute
- 完全脱离文档流,不再占据空间。
- 定位参照是最近的已定位祖先(position ≠ static)。
.parent {
position: relative; /* 关键:作为参照 */
}
.child {
position: absolute;
top: 0; /* 相对 .parent 左上角 */
z-index: 10;
}
- 若祖先都没有定位,则相对初始包含块(通常是浏览器视口,且会随页面滚动而移动,注意与 fixed 的区别)。
fixed
- 脱离文档流,始终相对视口定位,页面滚动时不会跟着滚动。
- 常用于:导航栏、返回顶部按钮、悬浮弹窗。
.nav {
position: fixed;
top: 0;
left: 0;
width: 100%;
z-index: 1000;
}
- 陷阱:如果祖先有 transform / perspective / filter 等属性,会创建新的包含块,fixed 会变为相对该祖先定位。
sticky(粘性定位)
- 是 relative 与 fixed 的结合体:
- 未滚动到阈值时 → 像 relative,在文档流中占据位置;
- 滚动到阈值后 → 像 fixed,黏在滚动容器内。
.header {
position: sticky;
top: 0; /* 滚动到顶距 0 时开始黏住 */
z-index: 100;
}
- 常见用途:表头吸顶、分类菜单吸顶。
- 注意:sticky 的滚动容器必须是最近的可滚动祖先;若祖先设置 overflow: hidden/auto 会限制其黏住范围。
z-index
1. 只对「定位元素」生效
- 只有 position: relative / absolute / fixed / sticky 时该属性才有意义。
2. 数值含义
- auto(默认):不参与层叠上下文创建,按文档顺序和定位类型排序
- 0:与 auto 类似,但会创建层叠上下文
- 正整数:数值越大,层级越高,显示越靠前
- 负整数(如 -1):可以低于普通(static)内容,甚至可以跑到父元素背景之下
.box-a { position: relative; z-index: 1; }
.box-b { position: relative; z-index: 999; }
/* box-b 覆盖 box-a */
.box-c { position: relative; z-index: -1; }
/* box-c 会跑到父元素背景层下面 */
3. 相同 z-index 时 → 看 DOM 顺序
- 如果层级相同,后面的元素覆盖前面的元素
<div class="box" style="position:relative; z-index:1; background:red;">A</div>
<div class="box" style="position:relative; z-index:1; background:blue; margin-left:50px;">B</div>
<!-- B 覆盖 A 的交叉部分 -->
4. 层叠上下文(Stacking Context)
- z-index 的比较只在同一个层叠上下文内有效。多种情况会创建层叠上下文:
- position:定位 + z-index != auto
- opacity < 1
- transform / perspective / filter / backdrop-filter 非 none
- mix-blend-mode 非 normal
- display: flex / grid 的子元素且 z-index != auto
- position: fixed / sticky(总是创建)
- contain: paint / layout、will-change 指定上述属性等
- 核心原则:子元素的 z-index 只在父级层叠上下文中比较,无法跨越父级 与外部元素比较。
/* 父级 context-a 的 z-index=1 */
.context-a { position: relative; z-index: 1; }
.inner-a { position: relative; z-index: 999; }
/* 父级 context-b 的 z-index=2 */
.context-b { position: relative; z-index: 2; }
.inner-b { position: relative; z-index: 1; }
/* 结果:即使 inner-a 有 999,整个 context-a 仍被 context-b 覆盖 */
DEMO
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>position 与 z-index 综合 Demo</title>
<style>
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
background: #f0f2f5;
color: #333;
padding: 20px;
}
.page-title {
text-align: center;
padding: 24px 0 4px;
font-size: 28px;
color: #1a1a1a;
}
.page-sub {
text-align: center;
color: #888;
margin-bottom: 30px;
font-size: 14px;
}
.demo-section {
background: #fff;
border-radius: 10px;
box-shadow: 0 2px 12px rgba(0, 0, 0, 0.06);
margin: 0 auto 30px;
max-width: 960px;
padding: 24px;
}
.demo-section h2 {
font-size: 20px;
margin-bottom: 6px;
color: #222;
}
.demo-section h2 .tag {
font-size: 13px;
background: #e8f0fe;
color: #1a73e8;
border-radius: 4px;
padding: 2px 8px;
margin-left: 8px;
vertical-align: middle;
font-weight: normal;
}
.demo-section > p.desc {
color: #777;
font-size: 14px;
margin-bottom: 16px;
line-height: 1.6;
}
.demo-area {
position: relative;
border: 2px dashed #d0d5dd;
border-radius: 8px;
padding: 16px;
min-height: 220px;
background: #fafbfc;
}
/* ========== Demo 1: 五种 position ========== */
.box {
display: inline-flex;
align-items: center;
justify-content: center;
color: #fff;
font-size: 13px;
font-weight: 600;
padding: 10px 14px;
border-radius: 6px;
text-align: center;
line-height: 1.4;
}
.box-static {
background: #64748b;
}
.box-static.try-offset {
top: 20px;
left: 40px;
}
.box-relative {
position: relative;
top: 20px;
left: 60px;
background: #3b82f6;
}
.abs-parent {
position: relative;
background: #eef2f7;
border: 1px solid #cbd5e1;
border-radius: 8px;
height: 130px;
margin-top: 20px;
padding: 50px 16px 0;
color: #94a3b8;
font-size: 13px;
}
.box-absolute {
position: absolute;
top: 12px;
right: 12px;
background: #f97316;
}
.box-fixed {
position: fixed;
right: 24px;
bottom: 24px;
background: #a855f7;
box-shadow: 0 4px 14px rgba(168, 85, 247, 0.45);
z-index: 100;
}
.tip {
font-size: 13px;
color: #b45309;
background: #fef3c7;
border: 1px solid #fde68a;
border-radius: 6px;
padding: 10px 12px;
margin-top: 12px;
line-height: 1.6;
}
/* ========== sticky ========== */
.sticky-scroller {
height: 200px;
overflow: auto;
border: 1px solid #cbd5e1;
border-radius: 8px;
background: #fff;
margin-top: 16px;
}
.sticky-bar {
position: sticky;
top: 0;
background: #ec4899;
color: #fff;
font-weight: 600;
padding: 10px 14px;
z-index: 10;
}
.sticky-content {
padding: 14px;
line-height: 2;
color: #666;
}
.sticky-content p {
margin: 0;
}
/* ========== Demo 2: z-index ========== */
.z-box {
position: absolute;
width: 170px;
height: 70px;
color: #fff;
border-radius: 6px;
display: flex;
align-items: center;
justify-content: center;
font-size: 13px;
font-weight: 600;
text-align: center;
padding: 6px;
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.15);
}
.same-z-a {
position: absolute;
width: 180px;
height: 90px;
background: #ef4444;
z-index: 1;
border-radius: 6px;
color: #fff;
display: flex;
align-items: center;
justify-content: center;
text-align: center;
font-size: 13px;
font-weight: 600;
padding: 6px;
}
.same-z-b {
position: absolute;
width: 180px;
height: 90px;
background: #3b82f6;
z-index: 1;
border-radius: 6px;
color: #fff;
display: flex;
align-items: center;
justify-content: center;
text-align: center;
font-size: 13px;
font-weight: 600;
padding: 6px;
}
/* ========== Demo 4: 层叠上下文 ========== */
.stack-wrap {
display: flex;
gap: 20px;
align-items: flex-start;
padding: 20px 0;
min-height: 220px;
}
.ctx {
position: relative;
width: 240px;
height: 170px;
border-radius: 10px;
color: rgba(255, 255, 255, 0.85);
font-size: 13px;
padding: 14px;
}
.ctx-a {
background: #4caf50;
z-index: 1;
}
.ctx-b {
background: #ff5722;
z-index: 2;
margin-left: -100px;
}
.ctx-inner {
position: absolute;
top: 55px;
width: 140px;
height: 65px;
border-radius: 8px;
color: #fff;
display: flex;
align-items: center;
justify-content: center;
font-weight: 700;
font-size: 12px;
text-align: center;
padding: 6px;
}
.ctx-inner-a {
left: 100px;
background: #1b5e20;
z-index: 999; /* 试图用超大数值“打败”外层 */
}
.ctx-inner-b {
left: 10px;
background: #bf360c;
z-index: 1;
}
</style>
</head>
<body>
<h1 class="page-title">CSS position 与 z-index 综合 Demo</h1>
<p class="page-sub">static / relative / absolute / fixed / sticky + z-index 层叠规则</p>
<!-- ========== Demo 1: position 五大取值 ========== -->
<section class="demo-section">
<h2>① position 五种取值 <span class="tag">滚动页面看效果</span></h2>
<p class="desc">
灰色为 static(设置了 top/left 但无效);蓝色为 relative(相对原位置偏移);
橙色为 absolute(相对最近的已定位祖先);紫色 fixed 固定在视口右下角(不随页面滚动)。
</p>
<div class="demo-area">
<div class="box box-static try-offset">
static<br>top/left 无效
</div>
<div class="box box-relative">
relative<br>top:20px left:60px
</div>
<div class="abs-parent">
.abs-parent(position: relative 作为参照)
<div class="box box-absolute">
absolute<br>top:12px right:12px
</div>
</div>
</div>
<p class="tip">
💡 补充陷阱:若 fixed 元素的祖先带有 <code>transform / filter / perspective</code>,
则 fixed 会相对该祖先定位,而不是视口。
</p>
</section>
<!-- ========== Demo 2: z-index 基础 ========== -->
<section class="demo-section">
<h2>② z-index 基础:数值 + DOM 顺序 <span class="tag">观察重叠块</span></h2>
<p class="desc">
左边:z-index 数值大的覆盖小的(1 < 2);右边:相同 z-index 时,DOM 靠后的覆盖靠前的。
</p>
<div class="demo-area">
<div class="z-box" style="background:#ef4444; z-index:1; left:20px; top:20px;">
z-index: 1<br>(红)
</div>
<div class="z-box" style="background:#3b82f6; z-index:2; left:70px; top:50px;">
z-index: 2<br>(蓝)覆盖红
</div>
<div class="same-z-a" style="left: 420px; top: 20px;">
同一 z-index:1<br>(第 1 个 DOM)
</div>
<div class="same-z-b" style="left: 470px; top: 55px;">
同一 z-index:1<br>(第 2 个 DOM,覆盖前者)
</div>
</div>
</section>
<!-- ========== Demo 3: 负 z-index 与 static ========== -->
<section class="demo-section">
<h2>③ 负 z-index 与 static <span class="tag">负值会沉底</span></h2>
<p class="desc">
static 元素无法使用 z-index;负 z-index 的定位元素会沉到普通内容之下,绿色正值则覆盖普通内容。
</p>
<div class="demo-area">
<p style="background:#cbd5e1; display:inline-block; padding:6px 12px; border-radius:6px; font-size:13px;">
普通 static 文字块(无 z-index)
</p>
<div class="z-box" style="background:#10b981; z-index:5; left:250px; top:20px;">
定位 z-index:5<br>盖住内容
</div>
<div class="z-box" style="background:#6366f1; z-index:-1; left:180px; top:40px;">
定位 z-index:-1<br>被普通内容盖住
</div>
</div>
</section>
<!-- ========== Demo 4: 层叠上下文 ========== -->
<section class="demo-section">
<h2>④ 层叠上下文:父级优先 <span class="tag">⚠️ 最常见的坑</span></h2>
<p class="desc">
绿色父容器 z-index:1,红色父容器 z-index:2。绿色内部的子元素写了 <code>z-index:999</code>,
依然无法越过红色父容器——因为子元素永远跳不出父级所在的层叠上下文。
</p>
<div class="stack-wrap">
<div class="ctx ctx-a">
父容器 A<br>z-index: 1
<div class="ctx-inner ctx-inner-a">
子元素 z-index: 999<br>(看似无敌)
</div>
</div>
<div class="ctx ctx-b">
父容器 B<br>z-index: 2
<div class="ctx-inner ctx-inner-b">
子元素 z-index: 1<br>(仍覆盖 A)
</div>
</div>
</div>
<p class="tip">
💡 z-index 的比较只在“同一个层叠上下文内”有效。子元素可以超过兄弟,
但永远不能跨越父容器与外部比拼。
</p>
</section>
<!-- ========== Demo 5: sticky 吸顶 ========== -->
<section class="demo-section">
<h2>⑤ sticky 吸顶演示 <span class="tag">滚动下方灰色区域</span></h2>
<p class="desc">
粉色条 <code>position: sticky; top: 0</code>,未到阈值时像 relative 一样在文档流中,
滚到滚动容器顶部后吸住不动。
</p>
<div class="sticky-scroller">
<div class="sticky-bar">我是 sticky 标题,滚到顶部就吸住 🧲</div>
<div class="sticky-content">
<p>──────────────────</p>
<p>第 1 行内容</p>
<p>第 2 行内容</p>
<p>第 3 行内容</p>
<p>第 4 行内容</p>
<p>第 5 行内容</p>
<p>──────────────────</p>
<p>继续向下滚动 ↓</p>
<p>继续向下滚动 ↓</p>
<p>继续向下滚动 ↓</p>
</div>
</div>
</section>
<!-- Demo 1 的固定元素放在最后,避免影响布局 -->
<div class="box box-fixed">
fixed<br>右下角
</div>
</body>
</html>
Flex 容器属性
display: flex 与主轴/交叉轴
- 设置
display: flex后,元素变为 flex 容器,直接子元素自动成为 flex item。 - 主轴(main axis):由 flex-direction 决定;
- 交叉轴(cross axis):与主轴垂直。
- justify-content 控制主轴对齐,align-items / align-content 控制交叉轴对齐。
flex-direction(主轴方向)
.container {
display: flex;
flex-direction: row; /* 默认:水平,从左到右 */
/* row-reverse | column | column-reverse */
}
- row(默认):主轴水平从左到右
- row-reverse:水平从右到左
- column:垂直从上到下
- column-reverse:垂直从下到上
flex-wrap(换行)
.container {
flex-wrap: nowrap; /* 默认:不换行,可能溢出 */
/* wrap | wrap-reverse */
}
- nowrap(默认):不换行
- wrap:换行,多行排列
- wrap-reverse:换行且反向排列
flex-flow(简写)
.container {
flex-flow: row wrap; /* flex-direction + flex-wrap */
}
justify-content(主轴对齐)
.container {
justify-content: flex-start; /* 默认 */
/* flex-end | center | space-between | space-around | space-evenly */
}
- flex-start(默认):起点对齐
- flex-end:终点对齐
- center:居中
- space-between:两端对齐,项目之间间距相等
- space-around:每个项目两侧间距相等
- space-evenly:项目之间与容器边缘间距完全相等
align-items(交叉轴对齐,单行)
.container {
align-items: stretch; /* 默认 */
/* flex-start | flex-end | center | baseline */
}
- stretch(默认):拉伸填满容器
- flex-start:交叉轴起点
- flex-end:交叉轴终点
- center:交叉轴居中
- baseline:文字基线对齐
align-content(交叉轴对齐,多行)
.container {
align-content: stretch; /* 默认 */
/* flex-start | flex-end | center | space-between | space-around */
}
- 仅当有多行(flex-wrap: wrap 生效)时有意义
- stretch:拉伸填满
- flex-start / flex-end / center:整体对齐
- space-between / space-around:行间分布
gap(间距)
.container {
gap: 12px; /* 行间距和列间距都是 12px */
row-gap: 8px; /* 行间距 */
column-gap: 16px; /* 列间距 */
}
Flex 项目属性
order(排列顺序)
.item {
order: 0; /* 默认 0,数值越小越靠前 */
}
- 默认所有项目 order 为 0,按 DOM 顺序排列
- 可以设为负数让项目排在最前
flex-grow(放大比例)
.item {
flex-grow: 0; /* 默认:不放大 */
}
- 默认 0,空间有剩余时也不放大
- 数值越大,分到的剩余空间越多
- 多个项目都为 1 时,平分剩余空间
flex-shrink(缩小比例)
.item {
flex-shrink: 1; /* 默认:可缩小 */
}
- 默认 1,空间不足时等比例缩小
- 设为 0:空间不足时也不缩小(容易溢出)
flex-basis(主轴初始大小)
.item {
flex-basis: auto; /* 默认:按内容或 width/height */
}
- 可以用 px / % / 关键字
- 优先级高于 width/height
flex(简写)
.item {
flex: 1; /* 相当于 1 1 0% */
flex: auto; /* 相当于 1 1 auto */
flex: none; /* 相当于 0 0 auto */
}
- 语法:
flex: <flex-grow> <flex-shrink> <flex-basis>
align-self(单独对齐)
.item {
align-self: auto; /* 默认:继承父容器 align-items */
/* flex-start | flex-end | center | baseline | stretch */
}
- 覆盖父容器的 align-items,只对当前项目生效
常见陷阱
- 子元素的 margin: auto 在 flex 布局中会吸收剩余空间,可用来快速居中
- flex 容器属性(justify-content / align-items)不作用于 flex 容器本身,只作用于它的直接子元素
- 如果项目没有设置 flex-basis,默认按内容大小计算;设置了 flex-grow 后才会填充剩余空间
unity
引擎窗口
layout
- 窗口快速布局
- save layout 可以保存自定义布局
Scene和Hierarchy
- 场景窗口和层级窗口息息相关
- 层级窗口看到的内容就是场景窗口中的显示对象
Hierarchy
- 创建拖入一下对象
- 右键或者+去快速创建对象
- F2对象改名
Scene
- 控制/查看对象
- 从左到右
- 渲染模式
- 视图模式
- 是否有光源、音效、特效
- 辅助线显示
- 辅助功能
- 搜索
- 右上还有一个场景轴向
- 红:x轴:平行屏幕向右为X正方向
- 绿:y轴:平行屏幕向上为Y正方向
- 蓝:z轴:垂直屏幕向内为Z正方向
- Q:平移
- W:移动
- E:旋转
- R:缩放
- T:2d
- Y:综合
