daily notes[45]

文章目錄

  • basic knowledge
  • references

basic knowledge

  1. the variable in Rust is not changed.
let x=5;
x=6;

Rust language promotes the concept that immutable variables are safer than variables in other programming language such as python and and are in favour of the concurrent operations.
of course,if actual coding requires more flexibility, the variable in Rust can be made mutable by adding the mut keyword.

let mut x: u32=5;
x=6;

why does Rust provide the concept of constants in addition to variable immutability?

const x:u32 =5;
  • To declare a constant, you need to use the const keyword, moreover, by convention, constants names should be defined in Capital letters separated by underscores,such as MAX_POINTS.

  • Constants cannot be declared with the mut keyword.

  • any constant must have an explicitly declared type.the following definition is incorrect.

const x=6;
  • the value of a constant must be completely defined during compilation,which is a major difference from immutable variables.
  • the following code can be compiled without any error.
let random_number = rand::random(); 

but the following code will encounter an error when compiled.

const random_number: u32  = rand::random(); 

by the way, Rust also has a variable type which is similar to constants,but the difference is it can be accessed in global scope and has a fixed memory address.

static STR_HELLO: &str = "Hello, world!"; // 不可變靜態變量
static mut COUNTER: u32 = 0; // 可變靜態變量

in addition,the static variable can be immutable ,but accessing them requires an unsafe block.

// 不可變靜態變量 - 安全訪問
static APP_NAME: &str = "MyApp"; // ? 安全// 可變靜態變量 - 需要 unsafe 訪問
static mut COUNTER: u32 = 0;     // ? 聲明為可變fn main() {// 訪問可變靜態變量需要 unsafeunsafe {COUNTER += 1; // ? 在 unsafe 塊中訪問println!("Counter: {}", COUNTER);}// COUNTER += 1; // ? 錯誤:不在 unsafe 塊中
}

when a variable needs to have various type in different scope ,you can use variable shadowing ,that is say,you can use let to delcare the same variable name again.

  1. Rust is a statically and strongly typed language.
categorytypeexplanation
scalari8, i16, i32, i64, i128, isize
u8, u16, u32, u64, u128, usize
signed/uinsigned integer,isize/usize depends on the machine architecture
f32, f64float/double number,by default as f64
boolboolean,truefalse
charUnicode scalar(4 bytes)
compound(T1, T2, T3, ...)tuple:it has Fixed length and can stores elements of the various types.
[T; N]array:it has Fixed length and stores elements of the same type. The data is stored on the stack.

furthermore,the dynamically resizable collections can use Vec<T>,String , and so on.they are allocated in the heap,but they are not primitive data types,in fact,they are part of standard library.

  1. function
charactergrammer exampleexplanation
function definitionfn function_name() { ... }use fn keyword
argumentsfn foo(x: i32, y: String)the arguments must explicitly declare type
return valuefn foo() -> i32 { ... }it is followed by -> to declare the return type.
implicitly returnfn foo() -> i32 { 5 }the return value is the last expression without semicolon in function body
explicitly returnfn foo() -> i32 { return 5; }to return early from a function,you can use return keyword.
function pointerlet f: fn(i32) -> i32 = my_function;function can be passed as value and used.
發散函數fn bar() -> ! { loop {} }! 標記,表示函數永不返回

4 . 控制語句

  • Rust 的控制語句主要分為兩類:條件分支 和 循環,它們都是表達式。
  • if 表達式允許你根據條件執行不同的代碼分支,格式為 :if…else或者if…else if…else…。
  • loop 關鍵字會創建一個無限循環,直到顯式地使用 break 跳出。可以在 break 后跟一個表達式,這個表達式將成為 loop 表達式的返回值。continue也可以像其它語言一樣使用。
  • while 循環在條件為 true 時持續執行。
  • for 循環遍歷集合。如下所示。
fn main() {let x = [11, 12, 13, 14, 15];for element in x {println!("the value is: {}", element);}// 或者使用迭代器for element in x.iter() {println!("the value is: {}", element);}
}
  • range 由標準庫提供,生成一個數字序列。
(1..4):生成 1, 2, 3(不包含結束值)。(1..=4):生成 1, 2, 3, 4(包含結束值)。(start..end).rev():可以反轉范圍。
  • match可進行模式匹配和窮盡性檢查。match根據一個值的不同情況,執行不同的代碼并返回相應的結果。
fn main() {let number = 3;// 使用 match 將數字轉換為星期幾let day = match number {1 => "星期一",2 => "星期二", 3 => "星期三",4 => "星期四",5 => "星期五",6 => "星期六",7 => "星期日",_ => "無效的數字,請輸入1-7", // _ 通配符處理所有其他情況};println!("數字 {} 對應的是: {}", number, day);
}

references

  1. https://www.rust-lang.org/learn/
  2. deepseek

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/bicheng/98145.shtml
繁體地址,請注明出處:http://hk.pswp.cn/bicheng/98145.shtml
英文地址,請注明出處:http://en.pswp.cn/bicheng/98145.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

技術奇點爆發周:2025 年 9 月科技突破全景掃描

技術奇點爆發周&#xff1a;2025 年 9 月科技突破全景掃描當中國 "祖沖之三號" 量子計算機在特定任務上超越經典超級計算機一千萬億倍的算力新聞&#xff0c;與 OpenAI 宣布 100 億美元定制芯片量產協議的消息在同一周密集爆發時&#xff0c;我們真切感受到了技術革命…

分布式專題——10.3 ShardingSphere實現原理以及內核解析

1 ShardingSphere-JDBC 內核工作原理當往 ShardingSphere 提交一個邏輯SQL后&#xff0c;ShardingSphere 到底做了哪些事情呢&#xff1f;首先要從 ShardingSphere 官方提供的這張整體架構圖說起&#xff1a;1.1 配置管控在 SQL 進入 ShardingSphere 內核處理&#xff08;如解析…

移動語義的里里外外:從 std::move 的幻象到性能的現實

我們都已經聽過這樣的建議&#xff1a;“使用 std::move 來避免昂貴的拷貝&#xff0c;提升性能。” 這沒錯&#xff0c;但如果你對它的理解僅止于此&#xff0c;那么你可能正在黑暗中揮舞著一把利劍&#xff0c;既可能披荊斬棘&#xff0c;也可能傷及自身。 移動語義是 C11 帶…

selenium完整版一覽

selenium 庫驅動瀏覽器selenium庫是一種用于Web應用程序測試的工具,它可以驅動瀏覽器執行特定操作,自動按照腳本代碼做出單擊、輸入、打開、驗證等操作,支持的瀏覽器包括IE、Firefox、Safari、Chrome、Opera等。而在辦公領域中如果經常需要使用瀏覽器操作某些內容,就可以使用se…

[Linux]學習筆記系列 -- lib/kfifo.c 內核FIFO實現(Kernel FIFO Implementation) 高效的無鎖字節流緩沖區

文章目錄lib/kfifo.c 內核FIFO實現(Kernel FIFO Implementation) 高效的無鎖字節流緩沖區歷史與背景這項技術是為了解決什么特定問題而誕生的&#xff1f;它的發展經歷了哪些重要的里程碑或版本迭代&#xff1f;目前該技術的社區活躍度和主流應用情況如何&#xff1f;核心原理與…

MFC_Install_Create

1. 安裝MFC 編寫MFC窗口應用程序需要用到Visual Studiohttps://visualstudio.microsoft.com/zh-hans/&#xff0c;然后安裝&#xff0c;要選擇使用C的桌面開發&#xff0c;再點擊右邊安裝詳細信息中的使用C的桌面開發&#xff0c;往下滑&#xff0c;有一個適用于最新的v143生成…

Langchain4j開發之AI Service

學習基于Langchain4j的大模型開發需要學習其中Ai Service的開發模式。里面對大模型做了一層封裝&#xff0c;提供一些可以方便調用的api。其中有兩種使用Ai Service的方式。一.編程式開發1.首先引入Langchain4的依賴。<dependency><groupId>dev.langchain4j</gr…

認識神經網絡和深度學習

什么是神經網絡&#xff1f;什么又是深度學習&#xff1f;二者有什么關系&#xff1f;……帶著這些疑問&#xff0c;進入本文的學習。什么是神經網絡神經網絡&#xff08;Neural Network&#xff09;是一種模仿生物神經系統&#xff08;如大腦神經元連接方式&#xff09;設計的…

醫療行業安全合規數據管理平臺:構建高效協作與集中化知識沉淀的一體化解決方案

在醫療行業中&#xff0c;數據不僅是日常運營的基礎&#xff0c;更是患者安全、服務質量和合規管理的核心載體。隨著醫療業務的復雜化和服務模式的多元化&#xff0c;各類機構——從大型醫院到科研中心——都面臨著海量文檔、報告、影像資料和政策文件的管理需求。這些資料往往…

Day25_【深度學習(3)—PyTorch使用(5)—張量形狀操作】

reshape() squeeze()unsqueeze()transpose()permute()view() reshape() contiguous() reshape() 一、reshape() 函數保證張量數據不變的前提下改變數據的維度&#xff0c;將其轉換成指定的形狀。def reshape_tensor():data torch.tensor([[1, 2, 3], [4, 5, 6]])print(data…

第十八篇 開發網頁教學:實現畫布、繪畫、簡易 PS 方案

在網頁開發領域&#xff0c;畫布功能是實現交互創作的重要基礎&#xff0c;無論是簡單的繪畫工具&#xff0c;還是具備基礎修圖能力的簡易 PS 方案&#xff0c;都能為用戶帶來豐富的視覺交互體驗。本篇教學將圍繞 “學習 - 實踐 - 實操” 的核心思路&#xff0c;從技術原理講解…

封裝形成用助焊劑:電子制造“隱形橋梁”的技術突圍與全球產業重構

在5G通信、人工智能、新能源汽車等新興技術驅動下&#xff0c;全球電子制造業正以年均6.8%的增速重構產業鏈。作為電子元件焊接的核心輔料&#xff0c;封裝形成用助焊劑&#xff08;又稱電子封裝用助焊劑&#xff09;憑借其“優化焊接質量、提升可靠性、降低制造成本”的核心價…

【完整源碼+數據集+部署教程】零件實例分割系統源碼和數據集:改進yolo11-GhostHGNetV2

背景意義 研究背景與意義 隨著工業自動化和智能制造的迅速發展&#xff0c;零件的高效識別與分割在生產線上的重要性日益凸顯。傳統的圖像處理方法在處理復雜場景時往往面臨著準確性不足和實時性差的問題&#xff0c;而深度學習技術的引入為這一領域帶來了新的機遇。特別是基于…

墨色規則與血色節點:C++紅黑樹設計與實現探秘

前言? 前幾天攻克了AVL樹&#xff0c;我們已然是平衡二叉樹的強者。但旅程還未結束&#xff0c;下一個等待我們的&#xff0c;是更強大、也更傳奇的**終極BOSS**——紅黑樹。它不僅是map和set的強大心臟&#xff0c;更是C STL皇冠上的明珠。準備好了嗎&#xff1f;讓我們一…

大數據時代時序數據庫選型指南:為何 Apache IoTDB 成優選(含實操步驟)

在數字經濟加速滲透的今天&#xff0c;工業物聯網&#xff08;IIoT&#xff09;、智慧能源、金融交易、城市運維等領域每天產生海量 “帶時間戳” 的數據 —— 從工業設備的實時溫度、電壓&#xff0c;到電網的負荷波動&#xff0c;再到金融市場的每秒行情&#xff0c;這類 “時…

MAZANOKE+cpolar讓照片存儲無上限

文章目錄前言1. 關于MAZANOKE2. Docker部署3. 簡單使用MAZANOKE4. 安裝cpolar內網穿透5. 配置公網地址6. 配置固定公網地址總結當工具開始理解用戶的需求痛點時&#xff0c;MAZANOKE與cpolar這對搭檔給出了“輕量化”的解決方案。它不追求浮夸的功能堆砌&#xff0c;卻用扎實的…

正則表達式 - 元字符

正則表達式中的元字符是具有特殊含義的字符&#xff0c;它們不表示字面意義&#xff0c;而是用于控制匹配模式。基本元字符. (點號)匹配除換行符(\n)外的任意單個字符示例&#xff1a;a.b 匹配 "aab", "a1b", "a b" 等^ (脫字符)匹配字符串的開始…

suricata源碼解讀-事務日志

注冊事務日志線程模塊 void TmModuleTxLoggerRegister (void) {tmm_modules[TMM_TXLOGGER].name "__tx_logger__";tmm_modules[TMM_TXLOGGER].ThreadInit OutputTxLogThreadInit;tmm_modules[TMM_TXLOGGER].Func OutputTxLog;tmm_modules[TMM_TXLOGGER].ThreadExi…

【CSS】層疊上下文和z-index

z-index 的作用范圍受“層疊上下文&#xff08;stacking context&#xff09;”影響。&#x1f539; 1. z-index 的基本作用 控制元素在 同一個層疊上下文&#xff08;stacking context&#xff09; 內的堆疊順序。值越大&#xff0c;顯示層級越靠上。&#x1f539; 2. 什么是層…

自動化腳本的降本增效實踐

一、自動化腳本的核心價值自動化腳本通過模擬人類操作完成重復性任務&#xff0c;其核心價值體現在三個維度&#xff1a;首先&#xff0c;在時間成本方面&#xff0c;標準化的數據處理流程可縮短90%以上的操作耗時&#xff1b;其次&#xff0c;在人力成本上&#xff0c;單個腳本…