To prevent it, we can use the combo of checked_sub and unwrap_or. checked_sub will return Option<self> which means if there is no overflow occurs, we get Some(substraction_result), else we will get None. unwarp_or can then give the result a default value if we get None or just unwrap Some(substraction_result) to return substraction_result. Let’s look at the code.
num_i32 = num_i32.checked_sub(1).unwrap_or(100); // 100 // Since it overflows, it will return the default value // that we give to unwrap_or which is 100 for this example
$ spl-token mint 9J7Bmg8Yx4dPsSiiQAvdHuwLS5dCf9ET6jyamwpaJUKR 1
注意,這裏其實你也可以 mint 1 個以上,也可以成功,但這樣這個 token 總量就會有 1 個以上,就不是所謂的 NFT 了。
成功 mint 1 個代幣後,我們馬上關閉該代幣的鑄造功能。
這個關閉事不能再打開的,如此就達成意義上的 NFT 了,每個 token 都是獨一無二。
命令:
1
$ spl-token authorize <代幣 ID> mint --disable
執行結果:
1 2 3 4 5 6
$ spl-token authorize 9J7Bmg8Yx4dPsSiiQAvdHuwLS5dCf9ET6jyamwpaJUKR mint --disable Updating 9J7Bmg8Yx4dPsSiiQAvdHuwLS5dCf9ET6jyamwpaJUKR Current mint authority: 46hytJBhguswo6S8fCcVtR85HEnb9nd1hwMxFWYnSHXc New mint authority: disabled
You may want to delegate stake to additional validators while your existing stake is not eligible for withdrawal. It might not be eligible because it is currently staked, cooling down, or locked up. To transfer tokens from an existing stake account to a new one
// Push string str.push_str("ust"); // capacity is doubled to 12 println!("Now str: {}", str);
// Check if empty println!("Is empty: {}", str.is_empty());
// Check if contains println!("Contains 'PHP'? {}", str.contains("PHP"));
// Replace substring with println!("Replace output: {}", str.replace("Rust", "Solana")); // replace is not a in-place operation // so value of str is still "Hello Rust"
// Iterate a string by word (whitespace) forwordinstr.split_whitespace() { println!("{}", word); }
// Create a string with capacity letstr_with_cap_10 = String::with_capacity(10); println!("Length: {} Capacity: {}", str_with_cap_10.len(), str_with_cap_10.capacity()); }
The above outputs the following:
1 2 3 4 5 6 7 8 9 10
Now str: Hello Length: 6 Capacity: 6 Now str: Hello R Now str: Hello Rust Is empty: false Contains 'PHP'? false Replace output: Hello Solana Hello Rust Length: 0 Capacity: 10
Use “+” to append string
1 2 3 4
lets1 = String::from("Hello"); lets2 = String::from(" Rust"); lets3 = s1 + &s2; // Note: s1 is no longer valid since it's ownership is moved to s3 // s3 = "Hello Rust"
// Placeholder for debug trait println!("{:?}", (100, false, "Rust")); }
The above outputs the following:
1 2 3 4 5 6
Hello Rust Rust is rusty Rust is a programming language, and Rust is efficient Rust supports concurrent programming Binary: 1010 Hex: a Octal: 12 (100, false, "Rust")