xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

Rust 字符串插值 All In One

Rust 字符串插值 All In One

string formatting

字符串格式化

https://doc.rust-lang.org/std/fmt/

https://doc.rust-lang.org/rust-by-example/hello/print/fmt.html

https://doc.rust-lang.org/book/ch08-02-strings.html#storing-utf-8-encoded-text-with-strings

demos

extern crate wasm_bindgen;

use wasm_bindgen::prelude::*;

#[wasm_bindgen]
extern {
  pub fn alert(s: &str);
}

#[wasm_bindgen]
pub fn greet(name: &str) {
  // Rust 字符串插值
  alert(&format!("Hello, {}!", name));
}


// fn 定义 function
fn add(left: usize, right: usize) -> usize {
  // {} 插值,占位符
  println!("\n🦀 left = {}, right = {}", left, right);
  println!("🦀🦀 left + right = {}", left + right);
  println!("🦀🦀🦀 left = {}, right = {}, left + right = {}", left, right, left + right);
  left + right
}

https://www.cnblogs.com/xgqfrms/p/16660263.html

rust 数字转化字符串

rust 字符串拼接数字

fn main () {
  println!("🦀 shadowing 重影,let 重新声明");
  // 变量
  let x = 5;
  let x = x + 1;
  let x = x * 2;
  println!("\nvalue = {}", x);
  // value = 12
  test();
}

fn test () {
  println!("\n🦀 shadowing 重影 & let 重新声明");
  // 变量重影
  let shadowing = 1;
  // let shadowing = shadowing + "2";
  // ❌ str.len(); ^^^^^^^^^ expected `&str`, found `usize`
  // let shadowing = &(String::from("shadowing is equal to") + shadowing);
  // ❌ shadowing); ^^^^^^^^^ expected `&str`, found integer
  // let shadowing = &(String::from("shadowing is equal to") + String::from(shadowing));
  // ❌ String::from(shadowing)); ^^^^^^^^^^^ expected `&str`, found struct `String`
  // ❌ help: consider borrowing here: `&String::from(shadowing)`
  // let shadowing = &(String::from("shadowing is equal to") + &String::from(shadowing));
  // ❌  &String::from(shadowing)); ^^^^^^^^^^^^ the trait `From<{integer}>` is not implemented for `String`
  // let shadowing = &(String::from("shadowing is equal to") + &String::from(shadowing));
  // rust 数字专字符串
  // let shadowing = &format("shadowing is equal to {}", shadowing);
  // &format("shadowing is equal to {}", shadowing); ^^^^^^ not a function
  // help: use `!` to invoke the macro
  // help: consider importing this function instead
  // use `std::fmt::format`;
  let shadowing = &format!("shadowing is equal to {}", shadowing);
  println!("\nshadowing = {}", shadowing);
  //
  // 可变变量赋值, 类型不可以改变
  let mut str = "123";
  // str = str.len();
  // str.len(); ^^^^^^^^^ expected `&str`, found `usize`
  println!("\nstring = {}", str);
  str = "2022";
  println!("\nstring = {}", str);
}

https://rustwiki.org/zh-CN/rust-by-example/conversion/string.html

refs

Python 字符串插值 All In One

https://www.cnblogs.com/xgqfrms/p/16596326.html



©xgqfrms 2012-2020

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2022-09-11 22:23  xgqfrms  阅读(185)  评论(4编辑  收藏  举报