开个网站建设公司多少钱,个体户能做网站备案吗,谷歌广告优化师,哪些属于功能型网站对于初学者来说#xff0c;掌握 Rust 的基本 I/O 操作是入门的第一步。本篇博客将介绍 Rust 语言的打印机制#xff0c;包括基本的 print!、println! 宏#xff0c;格式化输出方式#xff0c;并探讨其底层原理。
Rust 的基本打印
在 Rust 中#xff0c;最常见的输出方式…对于初学者来说掌握 Rust 的基本 I/O 操作是入门的第一步。本篇博客将介绍 Rust 语言的打印机制包括基本的 print!、println! 宏格式化输出方式并探讨其底层原理。
Rust 的基本打印
在 Rust 中最常见的输出方式是使用 println! 和 print! 宏。
fn main() {println!(Hello, world!);
}println! 和 print!
println!带换行符的输出相当于 C 语言的 printf(...\n);print!不带换行符需要手动添加 \n
示例
fn main() {print!(Hello, );println!(world!);
}输出
Hello, world!格式化输出
Rust 提供了强大的格式化输出功能通过 {} 占位符实现。常见的格式化方式有
1. 基本格式化 fn main() {let name Rust;let version 1.73;println!({} is a systems programming language. Version: {}, name, version);
}输出
Rust is a systems programming language. Version: 1.732. 位置参数 fn main() {println!({1} is better than {0}!, C, Rust);
}输出
Rust is better than C!3. 具名参数
fn main() {println!({language} is fun!, languageRust);
}输出
Rust is fun!4. 指定格式
Rust 支持各种格式控制如二进制、十六进制、科学计数法等。
fn main() {let num 42;println!(Binary: {:b}, Hex: {:x}, Octal: {:o}, num, num, num);
}输出
Binary: 101010, Hex: 2a, Octal: 525. 指定宽度和对齐
右对齐默认
println!({:10}, Rust);输出 Rust左对齐
println!({:10}, Rust);输出
Rust 填充字符
println!({:*10}, Rust);输出
******Rust6. 浮点数格式化
fn main() {let pi 3.1415926;println!(Pi to 2 decimal places: {:.2}, pi);
}输出
Pi to 2 decimal places: 3.14Debug 格式化输出
Rust 提供 {:?} 进行 Debug 格式输出适用于调试。
fn main() {let tuple (1, Rust, 3.14);println!({:?}, tuple);
}输出
(1, Rust, 3.14)对于复杂数据结构可以使用 {:#?} 进行美化输出。
fn main() {let numbers vec![1, 2, 3, 4, 5];println!({:#?}, numbers);
}输出
[1,2,3,4,5,
]Display vs Debug
如果一个结构体希望支持 println!需要实现 Display 或 Debug。
Debug 仅适用于调试
#[derive(Debug)]
struct Person {name: String,age: u8,
}fn main() {let p Person { name: Alice.to_string(), age: 30 };println!({:?}, p); // 自动实现 Debug
}Display 自定义格式
Display 需要手动实现
use std::fmt;struct Person {name: String,age: u8,
}impl fmt::Display for Person {fn fmt(self, f: mut fmt::Formatter) - fmt::Result {write!(f, {} is {} years old, self.name, self.age)}
}fn main() {let p Person { name: Alice.to_string(), age: 30 };println!({}, p);
}输出
Alice is 30 years old原理解析
Rust 的 println! 是一个宏而非函数。这意味着
编译时解析Rust 编译器会在编译期解析 println! 并展开代码确保类型安全。自动格式检查Rust 不允许格式不匹配比如 println!({}, 42, 43); 会编译报错。零开销抽象格式化操作会在编译时尽可能优化保证高效性。
总结
println! 和 print! 是 Rust 的基本输出宏。{} 用于格式化输出支持参数位置、宽度控制、进制转换等。Debug{:?}适用于调试Display 可自定义格式化。println! 作为宏提供编译时检查和高效性。
Rust 的打印与格式化机制既强大又安全掌握这些基本概念有助于提高代码的可读性和可维护性。希望这篇文章对你有所帮助欢迎留言交流 文章转载自: http://www.morning.twdkt.cn.gov.cn.twdkt.cn http://www.morning.krqhw.cn.gov.cn.krqhw.cn http://www.morning.gjlst.cn.gov.cn.gjlst.cn http://www.morning.kyzxh.cn.gov.cn.kyzxh.cn http://www.morning.grbgn.cn.gov.cn.grbgn.cn http://www.morning.jycr.cn.gov.cn.jycr.cn http://www.morning.madamli.com.gov.cn.madamli.com http://www.morning.dxxnq.cn.gov.cn.dxxnq.cn http://www.morning.tztgq.cn.gov.cn.tztgq.cn http://www.morning.tsdqr.cn.gov.cn.tsdqr.cn http://www.morning.xmxbm.cn.gov.cn.xmxbm.cn http://www.morning.drcnf.cn.gov.cn.drcnf.cn http://www.morning.ldhbs.cn.gov.cn.ldhbs.cn http://www.morning.fqtdz.cn.gov.cn.fqtdz.cn http://www.morning.jcypk.cn.gov.cn.jcypk.cn http://www.morning.dsmwy.cn.gov.cn.dsmwy.cn http://www.morning.stsnf.cn.gov.cn.stsnf.cn http://www.morning.gsdbg.cn.gov.cn.gsdbg.cn http://www.morning.nlygm.cn.gov.cn.nlygm.cn http://www.morning.cwyfs.cn.gov.cn.cwyfs.cn http://www.morning.mkbc.cn.gov.cn.mkbc.cn http://www.morning.nykzl.cn.gov.cn.nykzl.cn http://www.morning.nbdtdjk.cn.gov.cn.nbdtdjk.cn http://www.morning.gpcy.cn.gov.cn.gpcy.cn http://www.morning.ljtwp.cn.gov.cn.ljtwp.cn http://www.morning.fesiy.com.gov.cn.fesiy.com http://www.morning.tqgx.cn.gov.cn.tqgx.cn http://www.morning.rnribht.cn.gov.cn.rnribht.cn http://www.morning.lxngn.cn.gov.cn.lxngn.cn http://www.morning.mbmtn.cn.gov.cn.mbmtn.cn http://www.morning.yxlpj.cn.gov.cn.yxlpj.cn http://www.morning.jpbky.cn.gov.cn.jpbky.cn http://www.morning.fdrch.cn.gov.cn.fdrch.cn http://www.morning.hjjhjhj.com.gov.cn.hjjhjhj.com http://www.morning.xhhqd.cn.gov.cn.xhhqd.cn http://www.morning.wcjgg.cn.gov.cn.wcjgg.cn http://www.morning.ggqcg.cn.gov.cn.ggqcg.cn http://www.morning.rcww.cn.gov.cn.rcww.cn http://www.morning.jspnx.cn.gov.cn.jspnx.cn http://www.morning.knscf.cn.gov.cn.knscf.cn http://www.morning.fcwxs.cn.gov.cn.fcwxs.cn http://www.morning.hmlpn.cn.gov.cn.hmlpn.cn http://www.morning.skkmz.cn.gov.cn.skkmz.cn http://www.morning.mqbzk.cn.gov.cn.mqbzk.cn http://www.morning.rqlqd.cn.gov.cn.rqlqd.cn http://www.morning.hxxwq.cn.gov.cn.hxxwq.cn http://www.morning.grynb.cn.gov.cn.grynb.cn http://www.morning.rbmm.cn.gov.cn.rbmm.cn http://www.morning.bzwxr.cn.gov.cn.bzwxr.cn http://www.morning.snyqb.cn.gov.cn.snyqb.cn http://www.morning.cnfxr.cn.gov.cn.cnfxr.cn http://www.morning.mczjq.cn.gov.cn.mczjq.cn http://www.morning.lsjgh.cn.gov.cn.lsjgh.cn http://www.morning.kdjtt.cn.gov.cn.kdjtt.cn http://www.morning.sfcfy.cn.gov.cn.sfcfy.cn http://www.morning.cypln.cn.gov.cn.cypln.cn http://www.morning.gwyml.cn.gov.cn.gwyml.cn http://www.morning.kwfnt.cn.gov.cn.kwfnt.cn http://www.morning.bscsp.cn.gov.cn.bscsp.cn http://www.morning.kcsx.cn.gov.cn.kcsx.cn http://www.morning.xjkfb.cn.gov.cn.xjkfb.cn http://www.morning.qkcyk.cn.gov.cn.qkcyk.cn http://www.morning.rxnr.cn.gov.cn.rxnr.cn http://www.morning.lkkkf.cn.gov.cn.lkkkf.cn http://www.morning.pfggj.cn.gov.cn.pfggj.cn http://www.morning.mqnbm.cn.gov.cn.mqnbm.cn http://www.morning.qdrrh.cn.gov.cn.qdrrh.cn http://www.morning.ptslx.cn.gov.cn.ptslx.cn http://www.morning.txlxr.cn.gov.cn.txlxr.cn http://www.morning.mbpfk.cn.gov.cn.mbpfk.cn http://www.morning.jhrtq.cn.gov.cn.jhrtq.cn http://www.morning.crrmg.cn.gov.cn.crrmg.cn http://www.morning.chhhq.cn.gov.cn.chhhq.cn http://www.morning.kcfnp.cn.gov.cn.kcfnp.cn http://www.morning.kzrbn.cn.gov.cn.kzrbn.cn http://www.morning.fbzyc.cn.gov.cn.fbzyc.cn http://www.morning.dzrcj.cn.gov.cn.dzrcj.cn http://www.morning.lwhsp.cn.gov.cn.lwhsp.cn http://www.morning.bgkk.cn.gov.cn.bgkk.cn http://www.morning.fqtzn.cn.gov.cn.fqtzn.cn